javascript
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>
二、導(dǎo)入相關(guān)的jar包
三、編寫相關(guān)的類文件
package cn.itcast;
?
import java.util.Date;
?
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
?* 文件名????: CodeCurDate.java<br/>
?* 創(chuàng)建人????:涂作權(quán)<br/>
?* 日期時間: 2013-6-18下午02:09:20<br/>
?* 描述??????????:?創(chuàng)建一個要執(zhí)行任務(wù)的類,該類必須繼承QuartzJobBean規(guī)范<br/>
?* 版本號????: V1.0
?*/
publicclass CodeCurDate extends QuartzJobBean {
?
????? /**
????? ?* 以某個時間段為周期,循環(huán)執(zhí)行的方法
????? ?* 到大某個時間,要執(zhí)行的方法
????? ?*/
????? protectedvoidexecuteInternal(JobExecutionContextarg0)
???????????????????????? throws JobExecutionException {
?????????????? System.out.println("ppppppppppppppppppppppppppppppppp");
?????????????? System.out.println(new Date());
????? }
}
四、編寫相關(guān)的配置文件
<?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創(chuàng)建執(zhí)行任務(wù)的類的實(shí)例 -->
????? <beanid="codeCurDate"class="org.springframework.scheduling.quartz.JobDetailBean">
????? ?? <!--在spring中執(zhí)行任務(wù)的類的實(shí)例的創(chuàng)建,不是通過spring的普通的方法,而是把融合到其他JobDetailBean類中-->
????? ?? <property name="jobClass">
????? ????<value>cn.itcast.CodeCurDate</value>
????? ?? </property>
????? </bean>
?????
????? <!-- 2創(chuàng)建一個觸發(fā)器,整合執(zhí)行任務(wù)的類的實(shí)例和時間關(guān)聯(lián) -->
????? <beanid="codeCurDateTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
????? ? <!-- 注入要執(zhí)行任務(wù)的類的實(shí)例 -->
????? ? <property name="jobDetail"ref="codeCurDate"></property>
????? ? <!-- 配置該觸發(fā)器,在第一次啟動之前等待2秒,以毫秒為單位 -->
????? ? <property name="startDelay"value="2000"/>
????? ? <!--配置啟動后,每隔4秒執(zhí)行任務(wù)一次,以毫秒為單位-->
????? ? <property name="repeatInterval"value="4000"/>
????? </bean>
?????
????? <!-- 3注冊觸發(fā)器,啟動調(diào)度任務(wù) -->
????? <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">
????? ??? <property name="triggers">
????? ??????<list>
????? ?????????<refbean="codeCurDateTrigger"/>
????? ??????</list>
????? ??? </property>
????? </bean>
</beans>
五、啟動服務(wù)器,接著就可以看到控制臺中每隔一段時間就與輸出。
?
?
二、通過CronTrigerBean的方式實(shí)現(xiàn)的任務(wù)調(diào)度策略
首先:編寫任務(wù)執(zhí)行類
package cn.itcast;
?
import java.util.Date;
?
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
importorg.springframework.scheduling.quartz.QuartzJobBean;
/**
?*文件名???? : CodeCurDate.java<br/>
?*創(chuàng)建人???? :涂作權(quán)<br/>
?*日期時間:2013-6-18 下午02:54:57<br/>
?*描述?????????? :? <br/>
?*版本號???? :V1.0
?*/
public class CodeCurDate extendsQuartzJobBean {
?
????? /**
????? ?* 以某個時間段為周期,循環(huán)執(zhí)行的方法
????? ?* 到大某個時間,要執(zhí)行的方法
????? ?*/
????? protectedvoid executeInternal(JobExecutionContext arg0)
???????????????????????? throwsJobExecutionException {
?????????????? System.out.println("PPPPPPPPPPPPPPPPPPPPPPPP");
?????????????? System.out.println(newDate());
????? }
}
其次:在Spring的配置文件進(jìn)行配置,配置代碼如下:
<?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創(chuàng)建執(zhí)行任務(wù)的類的實(shí)例 -->
????? <beanid="codeCurDate"class="org.springframework.scheduling.quartz.JobDetailBean">
????? ?? <!--在spring中執(zhí)行任務(wù)的類的實(shí)例的創(chuàng)建,不是通過spring的普通的方法,而是把融合到其他JobDetailBean類中-->
????? ?? <property name="jobClass">
????? ????<value>cn.itcast.CodeCurDate</value>
????? ?? </property>
????? </bean>
?????
????? <!-- 2創(chuàng)建一個觸發(fā)器,整合執(zhí)行任務(wù)的類的實(shí)例和時間關(guān)聯(lián) -->
????? <beanid="codeCurDateTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">
????? ?? <!-- 注入執(zhí)行任務(wù)的類 -->
????? ?? <property name="jobDetail"ref="codeCurDate"/>
????? ?? <!-- 配置定時執(zhí)行任務(wù),9點(diǎn)45分將調(diào)用該觸發(fā)器的執(zhí)行 -->
????? ?? <property name="cronExpression"value="0 32 13 * * ?"/>
????? </bean>
????? ?
????? <!-- 3注冊觸發(fā)器,啟動調(diào)度任務(wù) -->
????? <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">
????? ??? <property name="triggers">
????? ??????<list>
????? ?????????<refbean="codeCurDateTrigger"/>
????? ??????</list>
????? ??? </property>
????? </bean>
</beans>
最后:在web.xml中配置相關(guān)數(shù)據(jù)
<?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>
?
?
?
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Spring整合的quartz任务调度的实现方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 香港股票如何开户
- 下一篇: Spring整合了CXF的一个appli