[英]Quartz scheduler in Tomcat 6, thread does not stop
對於我的webapp,我使用Quartz。 當我部署應用程序時,一切正常。 當我取消部署應用程序時,Quartz線程不會被銷毀。
日志是:
信息 :停止服務Catalina
嚴重 :Web應用程序[/ example]似乎已經啟動了一個名為[DefaultQuartzScheduler_Worker-1]的線程,但未能阻止它。 這很可能造成內存泄漏。 2010年7月12日下午6:30:40 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
任何人都可以告訴我如何強制這些線程的銷毀行動?
謝謝,
托馬索
我發現對我來說問題是石英正在關閉但是webapp沒有等到石英完成它才關閉所以Tomcat決定它已經讓線程運行並抱怨。
所以我像這樣管理我的調度程序:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
...do some stuff with the scheduler...
scheduler.shutdown(true);
注意關閉的boolean參數是至關重要的部分 。 如果刪除該true
以調用no-arg版本或將其設置為false
,那么您的webapp將不會等待石英在關閉之前甩掉。
TL; DR:調用scheduler.shutdown(true)
使您的webapp等待石英完成。
你是如何開始Quartz的?
假設您沒有使用像Spring這樣的方便包裝器,您可能希望在應用程序的web.xml中使用<listener>
,以便Quartz可以通知應用程序啟動和關閉。
我建議你使用2.x版本,並在web.xml
添加一個監聽器。
將以下方法添加到偵聽器:
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null && event!=null && event
.getServletContext()!=null) {
ServletContext context = event.getServletContext();
StdSchedulerFactory sch = (StdSchedulerFactory) context.getAttribute("org.quartz.impl.StdSchedulerFactory.KEY");
if(sch!=null){
try {
logger.debug("call quartz Scheduler.shutdown()");
Collection<Scheduler> col = sch.getAllSchedulers();
for(Scheduler s:col){
s.shutdown();
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.