日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring整合的quartz任务调度的实现方式

發(fā)布時間:2024/9/27 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring整合的quartz任务调度的实现方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、在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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。