• 解决spring整合quartz,tomcat关闭后,quartz还在运行


    最近项目需要定时任务,首先想到的是用quartz,于是用spring整合quartz,quartz整合spring,需要用到spring的support包,引进后就可以了:
    其中配置quartz的spring配置文件:
        <bean id="getMessageFromCenter" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="polling"></property>
            <property name="targetMethod" value="getMessageFromCenter"></property>
        </bean>
       
        <bean id="getMessageFromCenterTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail" ref="getMessageFromCenter"></property>
            <property name="cronExpression" value="0/20 * * * * ?"></property>
        </bean>
       
        <bean id="pollingFaction" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
            <property name="triggers">
                <list>
                    <ref bean="getMessageFromCenterTrigger"/>
                </list>
            </property>
        </bean>

    这样我是每隔20秒运行对于的方法。定时任务是执行了,但碰到个非常棘手的问题,就是每次点击tomcat的shutdown.bat时,tomcat容器并未完全关闭,quartz居然还在跑,进程中的tomcat居然也还在,但好多实例已destroy了,quartz一运行,一大堆的错误,这样虽然可以再启动tomcat,但原来那个tomcat还在进程中,一直跑着,内存还占着 ,这样一来,如果不知情的,内存会一直撑爆掉去的,纠结了几天,下面是错误详情:
    一月 09, 2013 10:53:21 上午 org.apache.catalina.loader.WebappClassLoader loadClass
    INFO: Illegal access: this web application instance has been stopped already.  Could not load org.springframework.core.NestedExceptionUtils.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
    java.lang.IllegalStateException
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1597)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
        at org.springframework.core.NestedRuntimeException.(NestedRuntimeException.java:45)
        at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:320)
        at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:199)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:546)

    一月 09, 2013 11:10:10 上午 org.apache.catalina.startup.Catalina stopServer
    SEVERE: Catalina.stop:
    java.net.ConnectException: Connection refused: connect
        at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
        at java.net.Socket.connect(Socket.java:579)
        at java.net.Socket.connect(Socket.java:528)
        at java.net.Socket.(Socket.java:425)
        at java.net.Socket.(Socket.java:208)
        at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:490)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:371)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:452)

    很明显,错误来源是来之quartz,tomcat容器关闭后,log4j已销毁了,quartz一运行,出错,log打日志,然后就会报错。很明显,关闭quartz是必须的,那为什么quartz不随容器关闭呢,至今没搞明白,尝试着下面的做法,在SchedulerFactoryBean的那个标签中添加一个destroy-method="destroy"这个destroy是他自己带的销毁方法,但试了,没用。后来想到加个监听器,进行销毁定时任务计划:
    public class ShutdownQuartz implements ServletContextListener{
        public void contextDestroyed(ServletContextEvent arg0) {
            try {
                // Get a reference to the Scheduler and shut it down
                Scheduler scheduler = (Scheduler) BeanFactory.getBean("pollingFaction");
                scheduler.shutdown(true);
                // Sleep for a bit so that we don't get any errors
                Thread.sleep(1000);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        public void contextInitialized(ServletContextEvent arg0) {
        }
    }

    上面那个
    BeanFactory.getBean();是我自己框架的获得bean的方法,也可以用别的方法。然后再web.xml添加一个监听器就OK了,问题解决
        <</font>listener>
            com.followman.train.common.ShutdownQuartz
  • 相关阅读:
    STM32的备份寄存器测试
    dsp6657的helloworld例程测试-第一篇
    Dennis Gabor与全息摄影
    Gabor filter与Gabor transform
    图像生成器:让电脑学习生成数字图像
    Haar-like feature和Haar wavelet
    《Wonderland: A Novel Abstraction-Based Out-Of-Core Graph Processing System》章明星
    小波分析及其应用
    Discrete cosine transform(离散余弦转换)
    psimpl_v7_win32_demo
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318016.html
Copyright © 2020-2023  润新知