2010-04-28 112 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