java Tomcat 6 中的 Quartz 调度器,线程不停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3230324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quartz scheduler in Tomcat 6, thread does not stop
提问by Tommaso Taruffi
for my webapp I use Quartz. When I deploy the app all is ok. When I undeploy the app, the Quartz thread is not destroyed.
对于我的网络应用程序,我使用 Quartz。当我部署应用程序时一切正常。当我取消部署应用程序时,Quartz 线程不会被破坏。
Log is:
日志是:
INFO: Stopping service Catalina
SEVERE: The web application [/example] appears to have started a thread named [DefaultQuartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak. Jul 12, 2010 6:30:40 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
信息:停止服务 Catalina
严重:Web 应用程序 [/example] 似乎已启动名为 [DefaultQuartzScheduler_Worker-1] 的线程,但未能将其停止。这很可能造成内存泄漏。2010 年 7 月 12 日下午 6:30:40 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
Anyone can tell me how I can force the destroy action for those threads?
任何人都可以告诉我如何强制对这些线程执行销毁操作?
Thanks,
谢谢,
Tommaso
托马索
采纳答案by matt b
How are you starting Quartz?
你如何开始石英?
Assuming you are not using a convenient wrapper like Spring, you probably want to be using a <listener>in your application's web.xml so that Quartz can be notified of the application start andshutdown.
假设您没有使用像 Spring 这样方便的包装器,您可能希望<listener>在应用程序的 web.xml 中使用 a以便 Quartz 可以在应用程序启动和关闭时得到通知。
See QuartzInitializerListeneror QuartzInitializerServletfor instance.
回答by Tom Saleeba
I found that the issue for me was that quartz was being shutdown but the webapp didn't wait for quartz to finish before it shutdown so Tomcat decided that it had left threads running and complained.
我发现我的问题是石英正在关闭,但 web 应用程序在关闭之前没有等待石英完成,所以 Tomcat 决定它让线程运行并抱怨。
So I managed my scheduler like this:
所以我像这样管理我的调度程序:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
...do some stuff with the scheduler...
scheduler.shutdown(true);
Note the boolean argument to shutdownis the vital part. If you remove that trueto call the no-arg version or set it to false, your webapp won't wait for quartz to shudown before it shuts down.
请注意,shutdown的布尔参数是至关重要的部分。如果您删除它true以调用 no-arg 版本或将其设置为false,则您的 web 应用程序将不会等待 Quartz 在关闭之前关闭。
TL;DR: call scheduler.shutdown(true)to make your webapp wait for quartz to finish.
TL;DR:调用scheduler.shutdown(true)让你的 web 应用程序等待 Quartz 完成。
回答by Yu Jiaao
I recommend you to use the 2.x version and, add a listener to web.xml.
我建议您使用 2.x 版本,并在web.xml.
Add the method below to the listener:
将以下方法添加到侦听器:
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();
}
}
}
}

