Spring配置quartz調度任務
1、SPring Bean注入作業的兩種方式:
方式一:@1、Spring使用JobDetailBean類,繼承QuartzJobBean,重寫
protected void executeInternal(JobExecutionContext context)方法
@2、 或實現Job接口,在execute方法中調用執行任務
方式二:Spring使用MethodInvokingJobDetailFactoryBean,普通Java類都可
2、方式一的實現:
java 代碼
public class AlterJob extends QuartzJobBean{
//業務實現
public void work() {
System.out.println("執行調度任務:"+new Date());
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
this.work();
}
//實現Job接口
// public void execute(JobExecutionContext arg0) throws JobExecutionException {
// this.work();
// }
}
applicationContext.xml的配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
">
<!-- 注入執行任務Bean -->
<bean name="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean" >
<property name="jobClass" value="com.boonya.quartz.job.AlterJob" />
<property name="jobDataAsMap">
<map>
<entry key="size" value="10"></entry>
</map>
</property>
<property name="applicationContextJobDataKey" value="applicationContext"/>
</bean>
<!-- 觸發器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="startDelay" value="20000"/>
<property name="repeatInterval" value="60000"/>
<property name="jobDetail" ref="jobDetailBean"/>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<!-- 指向我們的任務 -->
<property name="jobDetail" ref="jobDetailBean" />
<!-- 每分鐘10秒運行1次 -->
<property name="cronExpression" value="0/10 * * * * ?" />
</bean>
<!-- 調度器 -->
<!-- <bean id="startCornQuartzJob" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name="triggers">
<list>
觸發器列表
<ref bean="cronTrigger" />
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties" />
<property name="autoStartup" value="true"/>
<property name="schedulerName" value="cronScheduler" />
</bean> -->
<bean id="startSimpleQuartzJob" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
<ref bean="cronTrigger" />
</list>
</property>
<property name="autoStartup" value="true"/>
<property name="schedulerName" value="cronScheduler" />
</bean>
</beans>
注意:這里任務啟動的時候startCornQuartzJob和startSimpleQuartzJob不能同時都開放,否則拋出異常。
3、方式2的實現:
java代碼
public class QuartzJob {
public void work(){
System.out.println("work執行調度任務:"+new Date());
}
}
applicationContext.xml/applicationContext-jobs.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 要調用的工作類 -->
<bean id="quartzJob" class="com.boonya.quartz.job.QuartzJob"></bean>
<!-- 定義調用對象和調用對象的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 調用的類 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 調用類中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定義觸發時間 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<!-- cron表達式 -->
<property name="cronExpression">
<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
</property>
</bean>
<!-- 總管理類 如果將lazy-init='false'那么容器啟動就會執行調度程序 -->
<bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>
注:方式2的配置可以作為外部文件導入到applicationContext.xml中去。
測試方法二:
public static void main(String[] args) {
System.out.println("----------Test start.-------\n");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-jobs.xml");
//如果配置文件中將startQuertz bean的lazy-init設置為false 則不用實例化
context.getBean("startQuartz");
}
4、web.xml配置{跟以前沒有區別}
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<!-- 指定spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--加載多個spring配置文件 -->
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 定義SPRING監聽器,加載spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 字符過濾器 -->
<filter>
<filter-name>SetCharacterEncoding</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>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5、需要的Jar包:spring.jar,quartz.jar,若涉及持久化操作需要jpa.jar....等等,根據自己項目的需要添加。
搞了一天終于出來了,記于此處!
6、附上corn語法及示例
corn=[秒 分 時 日 月 周 年]
按順序依次為
秒(0~59)
分鐘(0~59)
小時(0~23)
天(月)(0~31,但是你需要考慮你月的天數)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
其中每個元素可以是一個值(如6),一個連續區間(9-12),一個間隔時間(8-18/4)(/表示每隔4小時),一個列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"這兩個元素互斥的,必須要對其中一個設置?.
0 0 10,14,16 * * ? 每天上午10點,下午2點,4點
0 0/30 9-17 * * ? 朝九晚五工作時間內每半小時
0 0 12 ? * WED 表示每個星期三中午12點
"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觸發
有些子表達式能包含一些范圍或列表
例如:子表達式(天(星期))可以為 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”
“*”字符代表所有可能的值
因此,“*”在子表達式(月)里表示每個月的含義,“*”在子表達式(天(星期))表示星期的每一天
“/”字符用來指定數值的增量
例如:在子表達式(分鐘)里的“0/15”表示從第0分鐘開始,每15分鐘
在子表達式(分鐘)里的“3/20”表示從第3分鐘開始,每20分鐘(它和“3,23,43”)的含義一樣
“?”字符僅被用于天(月)和天(星期)兩個子表達式,表示不指定值
當2個子表達式其中之一被指定了值以后,為了避免沖突,需要將另一個子表達式的值設為“?”
“L” 字符僅被用于天(月)和天(星期)兩個子表達式,它是單詞“last”的縮寫
但是它在兩個子表達式里的含義是不同的。
在天(月)子表達式中,“L”表示一個月的最后一天
在天(星期)自表達式中,“L”表示一個星期的最后一天,也就是SAT
如果在“L”前有具體的內容,它就具有其他的含義了
例如:“6L”表示這個月的倒數第6天,“FRIL”表示這個月的最一個星期五
注意:在使用“L”參數時,不要指定列表或范圍,因為這會導致問題
字段 允許值 允許的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小時 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可選) 留空, 1970-2099 , - * /</span>