简体   繁体   English

Tomcat 6中的Quartz调度程序,线程不会停止

[英]Quartz scheduler in Tomcat 6, thread does not stop

for my webapp I use Quartz. 对于我的webapp,我使用Quartz。 When I deploy the app all is ok. 当我部署应用程序时,一切正常。 When I undeploy the app, the Quartz thread is not destroyed. 当我取消部署应用程序时,Quartz线程不会被销毁。

Log is: 日志是:

INFO : Stopping service Catalina 信息 :停止服务Catalina

SEVERE : The web application [/example] appears to have started a thread named [DefaultQuartzScheduler_Worker-1] but has failed to stop it. 严重 :Web应用程序[/ example]似乎已经启动了一个名为[DefaultQuartzScheduler_Worker-1]的线程,但未能阻止它。 This is very likely to create a memory leak. 这很可能造成内存泄漏。 Jul 12, 2010 6:30:40 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 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 托马索

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. 我发现对我来说问题是石英正在关闭但是webapp没有等到石英完成它才关闭所以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 shutdown is the vital part . 注意关闭的boolean参数是至关重要的部分 If you remove that true to call the no-arg version or set it to false , your webapp won't wait for quartz to shudown before it shuts down. 如果删除该true以调用no-arg版本或将其设置为false ,那么您的webapp将不会等待石英在关闭之前甩掉。

TL;DR: call scheduler.shutdown(true) to make your webapp wait for quartz to finish. TL; DR:调用scheduler.shutdown(true)使您的webapp等待石英完成。

How are you starting Quartz? 你是如何开始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 and shutdown. 假设您没有使用像Spring这样的方便包装器,您可能希望在应用程序的web.xml中使用<listener> ,以便Quartz可以通知应用程序启动关闭。

See QuartzInitializerListener or QuartzInitializerServlet for instance. 例如,请参见QuartzInitializerListenerQuartzInitializerServlet

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();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM