[英]Spring scheduler shutdown error
在tomcat容器中開發基於SPRING的調度程序期間,我總是在取消部署webapp或shutdown服務器時獲得此logoutput:
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
我怎樣才能解決這個問題?
我將這個監聽器添加到我的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>
但錯誤仍然存在。
彈簧配置:
<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>
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 );
}
}
}
您需要添加一個關閉鈎子 - 請參閱在Spring 2.5中注冊一個關閉鈎子 。
在您的情況下,您可能應該向webapp添加一個上下文偵聽器來執行此操作(偵聽器+實現類的web.xml條目)。
使用close,這是最簡單的。
((YourClass)yourObject).close();
這是我的解決方案,因為我找到的在線工作都沒有。 這是專門用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)
{
}
確保線程終止的唯一方法是中斷並加入它們。
這可以通過實現org.quartz.InterruptableJob
來完成,如問題的答案中所述如何防止石英中的內存泄漏[?] 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.