Spring整合的quartz任務調度的實現方式
一、在web.xml中將配置文件的位置指定好。
Web.xml的配置如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
二、導入相關的jar包
三、編寫相關的類文件
package cn.itcast;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* 文件名 : CodeCurDate.java<br/>
* 創建人 :涂作權<br/>
* 日期時間: 2013-6-18下午02:09:20<br/>
* 描述 : 創建一個要執行任務的類,該類必須繼承QuartzJobBean規范<br/>
* 版本號 : V1.0
*/
publicclass CodeCurDateextends QuartzJobBean {
/**
* 以某個時間段為周期,循環執行的方法
* 到大某個時間,要執行的方法
*/
protectedvoidexecuteInternal(JobExecutionContextarg0)
throws JobExecutionException {
System.out.println("ppppppppppppppppppppppppppppppppp");
System.out.println(new Date());
}
}
四、編寫相關的配置文件
<?xmlversion="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 1創建執行任務的類的實例 -->
<beanid="codeCurDate"class="org.springframework.scheduling.quartz.JobDetailBean">
<!--在spring中執行任務的類的實例的創建,不是通過spring的普通的方法,而是把融合到其他JobDetailBean類中-->
<property name="jobClass">
<value>cn.itcast.CodeCurDate</value>
</property>
</bean>
<!-- 2創建一個觸發器,整合執行任務的類的實例和時間關聯 -->
<beanid="codeCurDateTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- 注入要執行任務的類的實例 -->
<property name="jobDetail"ref="codeCurDate"></property>
<!-- 配置該觸發器,在第一次啟動之前等待2秒,以毫秒為單位 -->
<property name="startDelay"value="2000"/>
<!--配置啟動后,每隔4秒執行任務一次,以毫秒為單位-->
<property name="repeatInterval"value="4000"/>
</bean>
<!-- 3注冊觸發器,啟動調度任務 -->
<beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<refbean="codeCurDateTrigger"/>
</list>
</property>
</bean>
</beans>
五、啟動服務器,接著就可以看到控制臺中每隔一段時間就與輸出。
二、通過CronTrigerBean的方式實現的任務調度策略
首先:編寫任務執行類
package cn.itcast;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
importorg.springframework.scheduling.quartz.QuartzJobBean;
/**
*文件名 : CodeCurDate.java<br/>
*創建人 :涂作權<br/>
*日期時間:2013-6-18 下午02:54:57<br/>
*描述 : <br/>
*版本號 :V1.0
*/
public class CodeCurDate extendsQuartzJobBean {
/**
* 以某個時間段為周期,循環執行的方法
* 到大某個時間,要執行的方法
*/
protectedvoid executeInternal(JobExecutionContext arg0)
throwsJobExecutionException {
System.out.println("PPPPPPPPPPPPPPPPPPPPPPPP");
System.out.println(newDate());
}
}
其次:在Spring的配置文件進行配置,配置代碼如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 1創建執行任務的類的實例 -->
<beanid="codeCurDate"class="org.springframework.scheduling.quartz.JobDetailBean">
<!--在spring中執行任務的類的實例的創建,不是通過spring的普通的方法,而是把融合到其他JobDetailBean類中-->
<property name="jobClass">
<value>cn.itcast.CodeCurDate</value>
</property>
</bean>
<!-- 2創建一個觸發器,整合執行任務的類的實例和時間關聯 -->
<beanid="codeCurDateTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 注入執行任務的類 -->
<property name="jobDetail"ref="codeCurDate"/>
<!-- 配置定時執行任務,9點45分將調用該觸發器的執行 -->
<property name="cronExpression"value="0 32 13 * * ?"/>
</bean>
<!-- 3注冊觸發器,啟動調度任務 -->
<beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<refbean="codeCurDateTrigger"/>
</list>
</property>
</bean>
</beans>
最后:在web.xml中配置相關數據
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>