弹簧调度程序关机错误

2022-09-03 15:26:43

在开发 tomcat 容器中基于 SPRING 的调度程序期间,我总是在取消部署 webapp 或关闭服务器上获得此登录输出:

Apr 28, 2010 4:21:33 PM org.apache.catalina.core.StandardService stop
INFO: Stopping service Catalina
Apr 28, 2010 4:21:33 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1] but has failed to stop it. This is very likely to create a memory leak.
Apr 28, 2010 4:21:33 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2] but has failed to stop it. This is very likely to create a memory leak.
Apr 28, 2010 4:21:33 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3] but has failed to stop it. This is very likely to create a memory leak.
Apr 28, 2010 4:21:33 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-4] but has failed to stop it. This is very likely to create a memory leak.
Apr 28, 2010 4:21:33 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-5] but has failed to stop it. This is very likely to create a memory leak.
.
.
.    
SEVERE: A web application created a ThreadLocal with key of type [org.springframework.core.NamedThreadLocal] (value [Prototype beans currently in creation]) and a value of type [null] (value [null]) but failed to remove it when the web application was stopped. To prevent a memory leak, the ThreadLocal has been forcibly removed.
Apr 28, 2010 4:21:34 PM org.apache.coyote.http11.Http11Protocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8606

我该如何解决这个问题?

谢谢史蒂夫布朗

我将此侦听器添加到我的 Web 应用

public class ShutDownHook implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        BeanFactory bf = (BeanFactory) ContextLoader.getCurrentWebApplicationContext();
        if (bf instanceof ConfigurableApplicationContext) {
            ((ConfigurableApplicationContext)bf).close();
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
    }
}

和我的网络.xml

<listener>
    <listener-class>pkg.utility.spring.ShutDownHook</listener-class>
</listener>

但错误仍然存在。

弹簧配置:

<bean id="run" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="concurrent" value="false" />
    <property name="targetObject" ref="scheduler" />
    <property name="targetMethod" value="task" />
</bean>

<bean id="cronTrg" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="run" />
    <property name="cronExpression" value="0/5 * * * * ?" />
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
    <property name="triggers">
        <list>
            <ref bean="cronTrg" />
        </list>
    </property>
</bean>

答案 1

恕我直言,这是石英调度程序的问题。我提交了一个错误 https://jira.terracotta.org/jira/browse/QTZ-192。作为一种解决方法,Colin Peters建议的sleep()解决方案对我有用。要不触发关闭两次,还可以将睡眠添加到Spring的SchpenderFactoryBean中:

import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

public class SchedulerFactoryBeanWithShutdownDelay extends SchedulerFactoryBean{

  @Override
  public void destroy() throws SchedulerException {
    super.destroy();
    // TODO: Ugly workaround for https://jira.terracotta.org/jira/browse/QTZ-192
    try {
      Thread.sleep( 1000 );
    } catch( InterruptedException e ) {
      throw new RuntimeException( e );
    }
  }
}

答案 2

这是我的解决方案,因为我在网上找到的解决方案都没有工作。这是专门用于关闭Spring & Tomcat的Quartz调度程序。

我的解释是:http://forum.springsource.org/showthread.php?34672-Quartz-doesn-t-shutdown&p=370060#post370060

基本上,问题似乎是Quartz没有足够的时间来干净地关闭,而waitForJobsToCompleteOnShutdown的论点似乎没有帮助。因此,我在 webapp 中实现了自定义关机侦听器,获取对调度程序的引用并手动将其关闭。然后等待1秒钟,然后再继续。

public class ShutDownHook implements ServletContextListener
{

    @Override
    public void contextDestroyed(ServletContextEvent arg0)
    {
        try
        {
            // Get a reference to the Scheduler and shut it down
            WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
            Scheduler scheduler = (Scheduler) context.getBean("quartzSchedulerFactory");
            scheduler.shutdown(true);

            // Sleep for a bit so that we don't get any errors
            Thread.sleep(1000);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0)
    {
    }

推荐