1. 程式人生 > >Spring-quartz使用以及動態修改cronExpression

Spring-quartz使用以及動態修改cronExpression

Quartz是一個強大的企業級任務排程框架。看下程式碼配置檔案Spring-quartz.xml程式碼:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/context http://www.springframework.org/schema/context/spring-context-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
	default-lazy-init="false">
<bean id="testJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass">
            <value>com.scdsys.main.test.web.controller.TestController</value>
        </property>
        <property name="jobDataAsMap">
		    <map>
			    <entry key="testService">
			      	<ref bean="testService"/>
			    </entry>
		    </map>
	    </property>
	</bean>
	
	<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
	     <property name="jobDetail">
	         <ref local="testJob"/>
	     </property>
	     <property name="cronExpression">
				<value>0 0 2 * * ?</value>
	     </property>
	</bean>
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	     <property name="triggers">
	         <list>
	             <ref local="testTrigger"/>
	         </list>
	     </property>
	</bean>

</beans>

上面是Controller的spring配置。
具體的Controller程式碼如下:

public class TestController extends QuartzJobBean{
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
			....//呼叫核心程式碼
		   }


CronExpression 觸發器利用一系列特殊字元,如下所示:

反斜線(/)字元表示增量值。例如,在秒欄位中“5/15”代表從第 5 秒開始,每 15 秒一次。

問號(?)字元和字母 L 字元只有在月內日期和周內日期欄位中可用。問號表示這個欄位不包含具體值。所以,如果指定月內日期,可以在周內日期欄位中插入“?”,表示周內日期值無關緊要。字母 L 字元是 last 的縮寫。放在月內日期欄位中,表示安排在當月最後一天執行。在周內日期欄位中,如果“L”單獨存在,就等於“7”,否則代表當月內周內日期的最後一個例項。所以“0L”表示安排在當月的最後一個星期日執行。

在月內日期欄位中的字母(W)字元把執行安排在最靠近指定值的工作日。把“1W”放在月內日期欄位中,表示把執行安排在當月的第一個工作日內。

井號(#)字元為給定月份指定具體的工作日例項。把“MON#2”放在周內日期欄位中,表示把任務安排在當月的第二個星期一。

星號(*)字元是通配字元,表示該欄位可以接受任何可能的值。
欄位 允許值 允許的特殊字元
秒 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 *


動態修改cronExpression



<bean id="schedulerFactory"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    
       <property name="triggers">
            <list>
                <ref local="xxxxJobTrigger"/> --把你需要配置的trigger放裡面
            </list>
        </property>    
</bean>

<bean id="xxxAction" class="com.xx.XxxAction" scope="prototype">
		<property name="scheduler">
            <ref bean="schedulerFactory" />    
        </property> 
</bean>
--Java XxxAction.java

private Scheduler scheduler;

public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
}

try {
			System.out.println(scheduler.getTriggerGroupNames().length);
			CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(
					"xxxxJobTrigger", Scheduler.DEFAULT_GROUP);
			System.out.println(trigger.getCronExpression());
      --動態設定時間
			trigger.setCronExpression("0 26 10 * * ?");
			scheduler.rescheduleJob("xxxxJobTrigger", Scheduler.DEFAULT_GROUP,
					trigger);
		} catch (SchedulerException e1) {
			e1.printStackTrace();
		} catch (ParseException e1) {
			e1.printStackTrace();
}



Quartz 2.x配置 如果spring是3.x 則使用2.x版本

<?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: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.1.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	
	<!-- 	quartz2.x配置 -->
	<!-- job的配置開始 -->  
    <bean id="myJobDetail"  
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">  
            <ref bean="testQurtzController" />  
        </property>  
        <property name="targetMethod">  
            <value>executeMethod</value>  
        </property>  
    </bean>
    	
	<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
	     <property name="jobDetail">
	         <ref local="myJobDetail"/>
	     </property>
	     <property name="cronExpression">
				<value>0 23 10 * * ?</value>
	     </property>
	</bean>
	
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	     <property name="triggers">
	         <list>
	             <ref local="testTrigger"/>
	         </list>
	     </property>
	</bean>
</beans>




package com.controller;


import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.dwr.Test;
import com.dwr.TestDAO;


@Controller
public class TestQurtzController{
	
	@Resource
	private TestDAO testDAO ;
	
    protected void executeMethod(){
		Test t = new Test();
		t.setId(1);
		System.out.println("I do my job now ! "+this.testDAO.getCount(t));
     }  
}