Spring的quartz定時器重復執行二次的問題解決


Spring的quartz定時器同一時刻重復執行二次的問題解決
最近用Spring的quartz定時器的時候,發現到時間后,任務總是重復執行兩次,在tomcat或jboss下都如此。
打印出他們的hashcode,發現是不一樣的,也就是說,在web容器啟動的時候,重復啟了兩個quartz線程。
研究下來發現quartz確實會加載兩次:
第一次:web容器啟動的時候,讀取applicationContext.xml文件時,會加載一次。
第二次:Spring本身會加載applicationContext.xml一次。
而我的quartz配置就是寫在applicationContext.xml文件里的。

解決辦法很簡單
先把quartz配置信息提取出來,單獨存成一個文件,比如applicationContext-quartz.xml 然后修改web.xml,讓web容器啟動時,可以加載該文件

這樣quartz只會在web容器啟動時加載一次,Spring不會再加載了。
 
web.xml配置如下: <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/applicationContext-timertask.xml</param-value>
  </context-param>
  <!-- 開啟監聽 -->
  <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
   </listener>
 
把定時器的配置單獨寫成一個配置文件,在web容器啟動的時候只加載一次
 
這個問題在Spring的TimerTask定時器下也會出現,解決方法也是如此,希望對各位大俠有所幫助
----------------------------------------------------------------------------------------------------------------------------參考以上。-----------------------------------------------------------
spring-scheduler.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
     xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
     default-lazy-init="false">
    <!-- 任務管理器 --> 
    <beans> 
        <!-- 要調用的工作類 --> 
        <bean id="quartzJob" class="com.xinwei.util.UpdateCurrentValueTask"></bean> 
        <!-- 定義調用對象和調用對象的方法 多個任務定義多個--> 
        <bean id="jobtask2" 
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
            <!-- 調用的類 --> 
            <property name="targetObject"> 
                <ref bean="quartzJob" /> 
            </property> 
            <!-- 調用類中的方法 --> 
            <property name="targetMethod"> 
                <value>doTask</value> 
            </property> 
        </bean> 
        <!-- 定義觸發時間 --> 
        <bean id="doTime2" 
            class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 
            <property name="jobDetail"> 
                <ref bean="jobtask2" /> 
            </property> 
            <!-- cron表達式  每天24:00運行一次 --> 
           <property name="cronExpression" value="0 0 0 * * ?" /> 
        </bean> 
        <!-- 總管理類 如果將lazy-init='false'那么容器啟動就會執行調度程序 --> 
        <bean id="startQuertz" lazy-init="false" autowire="no" 
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
            <property name="triggers"> 
                <list> 
                    <ref bean="doTime2"/> 
                </list> 
            </property> 
        </bean> 
    </beans>
</beans>

web.xml..
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">
     <display-name>security</display-name>
     <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>
                classpath*:/applicationContext.xml,
                classpath*:/applicationContext-shiro.xml,
              classpath*:/spring-scheduler.xml </param-value>
     </context-param>
     <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <!-- 主要注冊request,在普通類中獲取request對象 -->
     <listener>
          <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
     </listener>
     
     <filter>
           <filter-name>encodingFilter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
           <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
           </init-param>
           <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
           </init-param>
     </filter>
     <filter-mapping>
           <filter-name>encodingFilter</filter-name>
           <url-pattern>/*</url-pattern>
     </filter-mapping>
     <!-- 過濾參數,將前台傳輸的參數request. page.去掉 -->
      <filter>
           <filter-name>ParameterFilter</filter-name>
          <filter-class>com.xinwei.filter.ParameterFilter</filter-class>
           <init-param> 
             <param-name>blackListURL</param-name> <!-- 配置黑名單url 表示不走過濾器的url order:1 --> 
             <param-value> 
                 /js/**
                     /css/**
                     /styles/**
                     /images/**
                     /plugins/**
                     /fonts/**
                     /login/timeout
                     /logout
                     /views/**
             </param-value> 
         </init-param> 
     <init-param> 
             <param-name>whiteListURL</param-name> <!-- 配置白名單url 表示走過濾器的url order:2--> 
             <param-value> 
                 /**
             </param-value> 
         </init-param> 
     </filter>
     <filter-mapping>
           <filter-name>ParameterFilter</filter-name>
           <url-pattern>/*</url-pattern>
     </filter-mapping>
     
     
     
     <!-- pagination filter -->
     <filter>
           <filter-name>PageFilter</filter-name>
          <filter-class>com.xinwei.filter.PageFilter</filter-class>
           <init-param> 
             <param-name>blackListURL</param-name> <!-- 配置黑名單url 表示不走過濾器的url order:1 --> 
             <param-value></param-value> 
         </init-param> 
     <init-param> 
             <param-name>whiteListURL</param-name> <!-- 配置白名單url 表示走過濾器的url order:2--> 
             <param-value> 
                 /**/list
             </param-value> 
         </init-param> 
     </filter>
     <filter-mapping>
           <filter-name>PageFilter</filter-name>
           <url-pattern>/*</url-pattern>
     </filter-mapping>
     
     
     
     
     
     
     <!-- <filter> <filter-name>openEntityManagerInViewFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
           </filter> <filter-mapping> <filter-name>openEntityManagerInViewFilter</filter-name>
           <url-pattern>/*</url-pattern> </filter-mapping> -->
     <filter>
           <filter-name>shiroFilter</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
           <init-param>
                <param-name>targetFilterLifecycle</param-name>
                <param-value>true</param-value>
           </init-param>
     </filter>
     <filter-mapping>
           <filter-name>shiroFilter</filter-name>
           <url-pattern>/*</url-pattern>
     </filter-mapping>
     <servlet>
           <servlet-name>springServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:/spring-mvc.xml</param-value>
           </init-param>
           <init-param>
                <param-name>dispatchOptionsRequest</param-name>
                <param-value>true</param-value>
           </init-param>
           <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
           <servlet-name>springServlet</servlet-name>
           <url-pattern>/</url-pattern>
     </servlet-mapping>
     
     <servlet>
    <servlet-name>hessianServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/server/hessian-server.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
      </servlet>
     
      <servlet-mapping>
        <servlet-name>hessianServlet</servlet-name>
        <url-pattern>/hessian/*</url-pattern>
      </servlet-mapping>
     <session-config>
           <session-timeout>30</session-timeout>
     </session-config>
     <error-page>
           <exception-type>java.lang.Throwable</exception-type>
           <location>/WEB-INF/views/error/500.jsp</location>
     </error-page>
     <error-page>
           <error-code>500</error-code>
           <location>/WEB-INF/views/error/500.jsp</location>
     </error-page>
     <error-page>
           <error-code>404</error-code>
           <location>/WEB-INF/views/error/404.jsp</location>
     </error-page>
     <error-page>
           <error-code>403</error-code>
           <location>/WEB-INF/views/error/403.jsp</location>
     </error-page>
</web-app>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM