2010-04-28 110 views
14

在开发过程中在Tomcat容器中的基于弹簧的调度,我总是得到这个logoutput在取消部署Web应用程序或关闭服务器:春季调度关机错误

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 

我该如何解决这个问题?

谢谢stevedbrown

我这个监听器添加到我的webapp

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) { 
    } 
} 

和我的web.xml

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

但错误依然存在。

Spring配置:

<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> 
+0

嘿亚历克斯,你有这个问题的任何解决方案? – Joe 2011-01-11 17:39:31

回答

2

您需要添加一个关闭挂钩 - 见Registering a shutdown hook in Spring 2.5

在你的情况,你可能应该添加一个上下文监听器到你的webapp,这样做(监听器+实现类的web.xml条目)。

使用密切,这是最简单的。

((YourClass)yourObject).close(); 
+0

我添加了监听器,但没有任何事情发生。 – Alex 2010-04-28 19:42:20

+1

你想((ConfigurableApplicationContext)bf).close();;不((ConfigurableApplicationContext)bf).registerShutdownHook();除非你真的想用你的运行时注册关闭钩子[适用于junit](Runtime.getRuntime())。addShutdownHook(新的Thread(){ 公共无效的run(){ 如果(CTX的instanceof ConfigurableApplicationContext){ ((ConfigurableApplicationContext)CTX).close();} } }); ) – stevedbrown 2010-04-28 21:18:43

+0

我是盲目的,我必须关闭对象,我添加了弹簧配置并改变了我的侦听器类。 – Alex 2010-04-29 07:01:55

2

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

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

的问题似乎基本上什么是是,石英没有足够的时间来正常关闭和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) 
    { 
    } 
5

Imho这是一个石英调度程序的问题。我提出了一个错误https://jira.terracotta.org/jira/browse/QTZ-192。作为解决方案,Colin Peters建议的sleep()解决方案为我工作。 为了不触发关机两倍人们也可以睡眠添加到Spring的SchedulerFactoryBean来:

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

石英问题是用Quartz 2.1修复的。不幸的是,Spring 3.1基于Quartz 1.8,最早为Spring 3.2考虑更新为2.0。资料来源:https://jira.springsource.org/browse/SPR-7987 – StefanR 2011-09-21 06:00:21

+1

其实Spring 3.1确实支持quartz 2.x http://www.infoq.com/news/2011/10/spring-3.1-rc1-release – bmurmistro 2012-09-27 21:11:35

+0

我已升级到SpringFramework 4.1.6.RELEASE和Quartz 2.1.7,仍然需要做这个工作... – TungstenX 2015-06-11 07:31:02