SpringMvc整合Quartz實現定時任務項目源碼

         項目中一直使用Quartz做定時任務,但是一直沒有去仔細閱讀詳細參數配置說明以及它源碼,最近花了一點時間做了一個整理。系統的看了一下源碼實現並順手做了一個簡單的Demo。

一、說明

spring版本4.0.5、Quartz版本2.2.2
注意定時任務的配置需要spring-context-support-4.0.5.RELEASE.jar進行配置,所以項目中要加入此包。還需要log4j的包,這裏版本用的是log4j-1.2.17.jar,slf4j-log4j12-1.6.1.jar,slf4j-api-1.6.4.jar當然還有一些依賴包,項目中都有,這裏不再做說明。

二、安裝配置

1)JOBXML配置

<?xml version="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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName" default-lazy-init="true">
            
             <bean id="testJob" class="quartz.JobTest"></bean>     
        <!-- 定義調用對象和調用對象的方法 -->
        <bean id="testTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 調用的類 -->
            <property name="targetObject">
                <ref bean="testJob"/>
            </property>
            <!-- 調用類中的方法 -->
            <property name="targetMethod">
                <value>work</value>
            </property>
        </bean>  
        <!-- 調度觸發器 -->
        <bean id="jibDoTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail">
                <ref bean="testTask"/>
            </property>
            <!-- cron表達式 -->
            <property name="cronExpression">
                                        <value>0 0/1 * * * ?</value>
            </property>
        </bean> 
        <!-- 調度工廠  如果將lazy-init='false'那麼容器啓動就會執行調度程序 -->
        <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="jibDoTime"/>
                </list>
            </property>
            <!--必須的設置 QuartzScheduler 延時啓動,應用啓動完後 QuartzScheduler 再啓動 -->
            <property name="startupDelay" value="5" />
      </bean>
      <!-- 配置任務併發執行線程池 -->
          <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
                <property name="corePoolSize" value="3" />
                <property name="maxPoolSize" value="5" />
                <property name="queueCapacity" value="10" />
          </bean>
</beans>

2)代碼實現

package quartz;

public class JobTest {
        public void work(){
                System.out.println("任務開始執行");
        }
}

3)web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <display-name>acts_quartz</display-name>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-job.xml</param-value>
    </context-param>
     <!-- 監聽器 -->
         <listener>
                <listener-class>
                                org.springframework.web.context.ContextLoaderListener
                        </listener-class>
         </listener>
        <welcome-file-list>
                <welcome-file>index.html</welcome-file>
        </welcome-file-list>
</web-app>

關於cronExpression表達式: 
字段 允許值 允許的特殊字符 
秒 0-59 , - * / 
分 0-59 , - * / 
小時 0-23 , - * / 
日期 1-31 , - * ? / L W C 
月份 1-12 或者 JAN-DEC , - * / 
星期 1-7 或者 SUN-SAT , - * ? / L C # 
年(可選) 留空, 1970-2099 , - * / 
表達式意義 
"0 0 12 * * ?" 每天中午12點觸發 
"0 15 10 ? * *" 每天上午10:15觸發 
"0 15 10 * * ?" 每天上午10:15觸發 
"0 15 10 * * ? *" 每天上午10:15觸發 
"0 15 10 * * ? 2005" 2005年的每天上午10:15觸發 
"0 * 14 * * ?" 在每天下午2點到下午2:59期間的每1分鐘觸發 
"0 0/5 14 * * ?" 在每天下午2點到下午2:55期間的每5分鐘觸發 
"0 0/5 14,18 * * ?" 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發 
"0 0-5 14 * * ?" 在每天下午2點到下午2:05期間的每1分鐘觸發 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發 
"0 15 10 ? * MON-FRI" 週一至週五的上午10:15觸發 
"0 15 10 15 * ?" 每月15日上午10:15觸發 
"0 15 10 L * ?" 每月最後一日的上午10:15觸發 
"0 15 10 ? * 6L" 每月的最後一個星期五上午10:15觸發 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最後一個星期五上午10:15觸發 
"0 15 10 ? * 6#3" 每月的第三個星期五上午10:15觸發 
每天早上6點 
0 6 * * * 
每兩個小時 
0 */2 * * * 
晚上11點到早上8點之間每兩個小時,早上八點 
0 23-7/2,8 * * * 
每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點 
0 11 4 * 1-3 
1月1日早上4點 
0 4 1 1 * 

科幫網

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章