0
我的计划任务需要在部署在tomcat上的Web应用程序中运行。我使用Spring引导与Java 8Spring Scheduler线程无法在tomcat中关闭
我做什么,
配置类
@Configuration
@EnableScheduling
class SchedulerConfiguration implements SchedulingConfigurer {
@ConditionalOnProperty(name = "ScheduleJob1Switch", havingValue = "on")
@Bean
FirstScheduledJob firstScheduledJob() {
return new FirstScheduledJob();
}
@ConditionalOnProperty(name = "ScheduleJob2Switch", havingValue = "on")
@Bean
SecondScheduledJob secondScheduledJob() {
return new SecondScheduledJob();
}
@Bean(destroyMethod = "shutdown")
Executor taskExecutors() {
return Executors.newScheduledThreadPool(10);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutors());
}
}
JOB -1
public class FirstScheduledJob {
private static final String CLASS_NAME = FirstScheduledJob.class.getName();
@Autowired
private SubHandlerDAO subHandlerDAO;
@Autowired
private LookupService lookupService;
@Scheduled(cron = "${FirstScheduledJobCron}", zone = "UTC")
void firstJob() throws Exception {
//Do things
}
}
JOB-2也相似
所有的工作正常,除了当我尝试停止应用程序。 Tomcat日志说,在我创建的10个线程中有7-8个线程存在内存泄漏。日志:
26-Feb-2016 17:02:04.074 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [sample-job-handler-1.0.0] appears to have started a thread named [pool-1-thread-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(Unknown Source)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
这是一个线程的日志。其他人也一样。 为什么destroyMethod="shutdown"
在Executor
不起作用?
请参阅此问题以正确使用关机:http://stackoverflow.com/questions/36644043/how-to-forcefully-shutdown-java-executorservice/36644320#36644320 –