我使用的是石英版2.2.2
和Spring Boot版本1.3.1
。它表现得如此,一切正常。但是当我尝试关闭此应用程序时出现问题。日志显示存在内存泄漏......
我的Quartz配置;
org.quartz.scheduler.instanceName = my-app-jobs
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.threadCount=2
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.scheduler.skipUpdateCheck = true
我有2个类,它们实现如下的作业;
/**
*
* To Run Every 15 Minutes
*
*/
public class MemoryDataUpdateJob implements Job {
@Autowired
private Dao dao;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
Logger.info(getClass().getName(), "Scheduled Memory Data Update Job Started...");
//Read Device Settings
dao.readDeviceSettings();
Logger.info(getClass().getName(), "Scheduled Memory Data Update Job Finished.");
}
}
其他类也做类似的事情。
Scheduler Factory Bean如下;
@Bean
public SchedulerFactoryBean schedulerFactoryBean(
JobFactory jobFactory,
@Qualifier("MemoryDataUpdateJobTrigger") Trigger memoryDataUpdateJobTrigger,
@Qualifier("MsgCountJobTrigger") Trigger msgCountJobTrigger) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setJobFactory(jobFactory);
factory.setQuartzProperties(quartzProperties());
factory.setTriggers(memoryDataUpdateJobTrigger, msgCountJobTrigger);
return factory;
}
我正在获取下面的tomcat日志;
19-Jan-2016 11:18:39.722 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MyApp-1.0.0] appears to have started a thread named [schedulerFactoryBean_Worker-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
19-Jan-2016 11:18:39.722 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MyApp-1.0.0] appears to have started a thread named [schedulerFactoryBean_Worker-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
通过石英文档并在属性中添加以下内容;
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
第一个线程消息消失了,但关于worker-2
内存泄漏的第二条消息仍然存在。
但在我的应用程序记录器中,我看到类似的东西;
[2016-01-19 11:18:39:681] [DEBUG] [schedulerFactoryBean_Worker-1] [org.quartz.simpl.SimpleThreadPool:612] - WorkerThread is shut down.
[2016-01-19 11:18:40:047] [DEBUG] [schedulerFactoryBean_Worker-2] [org.quartz.simpl.SimpleThreadPool:612] - WorkerThread is shut down.
[2016-01-19 11:18:40:049] [INFO ] [Quartz Shutdown-Hook schedulerFactoryBean] [org.quartz.plugins.management.ShutdownHookPlugin:126] - Shutting down Quartz...
这是什么问题?即使应用程序记录器显示线程已关闭,为什么会出现内存泄漏?
答案 0 :(得分:2)
知道了。问题是tomcat正在关闭,但石英工作正在运行。如果我们等待一段时间,如1秒,那么它会正常关闭。我们可以通过扩展SchedulerFactoryBean
并覆盖它的destroy
方法来实现。像下面的东西;
public class SchedulerFactoryBeanWithWait extends SchedulerFactoryBean {
@Override
public void destroy() throws SchedulerException {
super.destroy();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logError(e);
}
}
}
和bean声明;
@Bean
public SchedulerFactoryBeanWithWait schedulerFactoryBeanWithWait (
JobFactory jobFactory,
@Qualifier("MemoryDataUpdateJobTrigger") Trigger memoryDataUpdateJobTrigger,
@Qualifier("MsgCountJobTrigger") Trigger msgCountJobTrigger) throws IOException {
SchedulerFactoryBeanWithWait factory = new SchedulerFactoryBeanWithWait ();
factory.setJobFactory(jobFactory);
factory.setQuartzProperties(quartzProperties());
factory.setTriggers(memoryDataUpdateJobTrigger, msgCountJobTrigger);
return factory;
}
这使它等到线程关闭。也许它将来会帮助别人。
答案 1 :(得分:1)
SchedulerFactoryBean 至少从 Spring 3.0 开始就有一个标志告诉它等待作业完成。设置标志并确保将在关闭时调用 destroy 方法:
@Bean(destroyMethodName="destroy")
public SchedulerFactoryBean schedulerFactoryBean(
JobFactory jobFactory,
@Qualifier("MemoryDataUpdateJobTrigger") Trigger memoryDataUpdateJobTrigger,
@Qualifier("MsgCountJobTrigger") Trigger msgCountJobTrigger) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setWaitForJobsToCompleteOnShutdown(true);
factory.setJobFactory(jobFactory);
factory.setQuartzProperties(quartzProperties());
factory.setTriggers(memoryDataUpdateJobTrigger, msgCountJobTrigger);
return factory;
}
或使用 XML 配置:
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
destroy-method="destroy">
<property name="waitForJobsToCompleteOnShutdown" value="true" />
....
</bean>