Spring Boot优雅关机报dataSource already closed
项目中有一个功能,是使用定时任务采集数据,在使用优雅关机功能时会出现数据源早于线程池中的线程关闭,从而导致 Caused by: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Tue Mar 01 12:19:07 CST 2022
错误,尝试以下方式可以解决上述问题,其逻辑是在监听到程序关闭事件时首先阻塞关闭连接池 shutdown()
。
@Slf4j
@Component
public class ApplicationEventListener implements ApplicationListener<ApplicationEvent> {
@Resource
private ThreadPoolTaskExecutor collectThreadPoolExecutor;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextClosedEvent) {
log.info("collectThreadPoolExecutor.shutdown().......");
collectThreadPoolExecutor.shutdown();
}
}
}
上次更新: 2024/01/31, 11:46:46