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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

javascript

Spring定时任务的几种实现方式

發(fā)布時(shí)間:2023/12/20 javascript 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring定时任务的几种实现方式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??

? ? ? ? 近日項(xiàng)目開(kāi)發(fā)中需要執(zhí)行一些定時(shí)任務(wù),比如需要在每天凌晨更新當(dāng)天簽到狀態(tài)為未簽到。從網(wǎng)上找了找 ,覺(jué)得這篇總結(jié)的較為詳盡,就先轉(zhuǎn)載下方便以后查閱。感謝原作者的歸納和分享。

一.分類

  • 從實(shí)現(xiàn)的技術(shù)上來(lái)分類,目前主要有三種技術(shù)(或者說(shuō)有三種產(chǎn)品):

  • Java自帶的java.util.Timer類,這個(gè)類允許你調(diào)度一個(gè)java.util.TimerTask任務(wù)。使用這種方式可以讓你的程序按照某一個(gè)頻度執(zhí)行,但不能在指定時(shí)間運(yùn)行。一般用的較少,這篇文章將不做詳細(xì)介紹。
  • 使用Quartz,這是一個(gè)功能比較強(qiáng)大的的調(diào)度器,可以讓你的程序在指定時(shí)間執(zhí)行,也可以按照某一個(gè)頻度執(zhí)行,配置起來(lái)稍顯復(fù)雜,稍后會(huì)詳細(xì)介紹。
  • Spring3.0以后自帶的task,可以將它看成一個(gè)輕量級(jí)的Quartz,而且使用起來(lái)比Quartz簡(jiǎn)單許多,稍后會(huì)介紹。
    • 從作業(yè)類的繼承方式來(lái)講,可以分為兩類:

  • 作業(yè)類需要繼承自特定的作業(yè)類基類,如Quartz中需要繼承自org.springframework.scheduling.quartz.QuartzJobBean;java.util.Timer中需要繼承自java.util.TimerTask。
  • 作業(yè)類即普通的java類,不需要繼承自任何基類。
  • 注:個(gè)人推薦使用第二種方式,因?yàn)檫@樣所以的類都是普通類,不需要事先區(qū)別對(duì)待。

    • 從任務(wù)調(diào)度的觸發(fā)時(shí)機(jī)來(lái)分,這里主要是針對(duì)作業(yè)使用的觸發(fā)器,主要有以下兩種:

  • 每隔指定時(shí)間則觸發(fā)一次,在Quartz中對(duì)應(yīng)的觸發(fā)器為:org.springframework.scheduling.quartz.SimpleTriggerBean
  • 每到指定時(shí)間則觸發(fā)一次,在Quartz中對(duì)應(yīng)的調(diào)度器為:org.springframework.scheduling.quartz.CronTriggerBean
  • 注:并非每種任務(wù)都可以使用這兩種觸發(fā)器,如java.util.TimerTask任務(wù)就只能使用第一種。Quartz和spring task都可以支持這兩種觸發(fā)條件。

    ?

    ?

    二.用法說(shuō)明

    詳細(xì)介紹每種任務(wù)調(diào)度工具的使用方式,包括Quartz和spring task兩種。

    Quartz

    第一種,作業(yè)類繼承自特定的基類:org.springframework.scheduling.quartz.QuartzJobBean。

    第一步:定義作業(yè)類

    ?

    Java代碼 ?
  • import?org.quartz.JobExecutionContext;??
  • import?org.quartz.JobExecutionException;??
  • import?org.springframework.scheduling.quartz.QuartzJobBean;??
  • public?class?Job1?extends?QuartzJobBean?{??
  • ??
  • private?int?timeout;??
  • private?static?int?i?=?0;??
  • //調(diào)度工廠實(shí)例化后,經(jīng)過(guò)timeout時(shí)間開(kāi)始執(zhí)行調(diào)度??
  • public?void?setTimeout(int?timeout)?{??
  • this.timeout?=?timeout;??
  • }??
  • ??
  • /**?
  • *?要調(diào)度的具體任務(wù)?
  • */??
  • @Override??
  • protected?void?executeInternal(JobExecutionContext?context)??
  • throws?JobExecutionException?{??
  • ??System.out.println("定時(shí)任務(wù)執(zhí)行中…");??
  • }??
  • }??
  • ?第二步:spring配置文件中配置作業(yè)類JobDetailBean

    Xml代碼 ?
  • <bean?name="job1"?class="org.springframework.scheduling.quartz.JobDetailBean">??
  • <property?name="jobClass"?value="com.gy.Job1"?/>??
  • <property?name="jobDataAsMap">??
  • <map>??
  • <entry?key="timeout"?value="0"?/>??
  • </map>??
  • </property>??
  • </bean>??
  • ?說(shuō)明:org.springframework.scheduling.quartz.JobDetailBean有兩個(gè)屬性,jobClass屬性即我們?cè)趈ava代碼中定義的任務(wù)類,jobDataAsMap屬性即該任務(wù)類中需要注入的屬性值。

    第三步:配置作業(yè)調(diào)度的觸發(fā)方式(觸發(fā)器)

    Quartz的作業(yè)觸發(fā)器有兩種,分別是

    org.springframework.scheduling.quartz.SimpleTriggerBean

    org.springframework.scheduling.quartz.CronTriggerBean

    第一種SimpleTriggerBean,只支持按照一定頻度調(diào)用任務(wù),如每隔30分鐘運(yùn)行一次。

    配置方式如下:

    ?

    Xml代碼 ?
  • <bean?id="simpleTrigger"?class="org.springframework.scheduling.quartz.SimpleTriggerBean">??
  • <property?name="jobDetail"?ref="job1"?/>??
  • <property?name="startDelay"?value="0"?/><!--?調(diào)度工廠實(shí)例化后,經(jīng)過(guò)0秒開(kāi)始執(zhí)行調(diào)度?-->??
  • <property?name="repeatInterval"?value="2000"?/><!--?每2秒調(diào)度一次?-->??
  • </bean>??
  • 第二種CronTriggerBean,支持到指定時(shí)間運(yùn)行一次,如每天12:00運(yùn)行一次等。

    配置方式如下:

    Xml代碼 ?
  • <bean?id="cronTrigger"?class="org.springframework.scheduling.quartz.CronTriggerBean">??
  • <property?name="jobDetail"?ref="job1"?/>??
  • <!—每天12:00運(yùn)行一次?-->??
  • <property?name="cronExpression"?value="0?0?12?*?*??"?/>??
  • </bean>??
  • ?關(guān)于cronExpression表達(dá)式的語(yǔ)法參見(jiàn)附錄。

    第四步:配置調(diào)度工廠?

    Xml代碼 ?
  • <bean?class="org.springframework.scheduling.quartz.SchedulerFactoryBean">??
  • <property?name="triggers">??
  • <list>??
  • <ref?bean="cronTrigger"?/>??
  • </list>??
  • </property>??
  • </bean>??
  • ?說(shuō)明:該參數(shù)指定的就是之前配置的觸發(fā)器的名字。

    第五步:啟動(dòng)你的應(yīng)用即可,即將工程部署至tomcat或其他容器。

    ?

    ?

    第二種,作業(yè)類不繼承特定基類。

    Spring能夠支持這種方式,歸功于兩個(gè)類:

    org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean

    org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

    這兩個(gè)類分別對(duì)應(yīng)spring支持的兩種實(shí)現(xiàn)任務(wù)調(diào)度的方式,即前文提到到j(luò)ava自帶的timer task方式和Quartz方式。這里我只寫(xiě)MethodInvokingJobDetailFactoryBean的用法,使用該類的好處是,我們的任務(wù)類不再需要繼承自任何類,而是普通的pojo。

    第一步:編寫(xiě)任務(wù)類

    Java代碼 ?
  • public?class?Job2?{??
  • public?void?doJob2()?{??
  • System.out.println("不繼承QuartzJobBean方式-調(diào)度進(jìn)行中...");??
  • }??
  • }??
  • ?可以看出,這就是一個(gè)普通的類,并且有一個(gè)方法。

    第二步:配置作業(yè)類

    Xml代碼 ?
  • <bean?id="job2"??
  • class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">??
  • <property?name="targetObject">??
  • <bean?class="com.gy.Job2"?/>??
  • </property>??
  • <property?name="targetMethod"?value="doJob2"?/>??
  • <property?name="concurrent"?value="false"?/><!--?作業(yè)不并發(fā)調(diào)度?-->??
  • </bean>??
  • ?說(shuō)明:這一步是關(guān)鍵步驟,聲明一個(gè)MethodInvokingJobDetailFactoryBean,有兩個(gè)關(guān)鍵屬性:targetObject指定任務(wù)類,targetMethod指定運(yùn)行的方法。往下的步驟就與方法一相同了,為了完整,同樣貼出。

    第三步:配置作業(yè)調(diào)度的觸發(fā)方式(觸發(fā)器)

    Quartz的作業(yè)觸發(fā)器有兩種,分別是

    org.springframework.scheduling.quartz.SimpleTriggerBean

    org.springframework.scheduling.quartz.CronTriggerBean

    第一種SimpleTriggerBean,只支持按照一定頻度調(diào)用任務(wù),如每隔30分鐘運(yùn)行一次。

    配置方式如下:

    Xml代碼 ?
  • <bean?id="simpleTrigger"?class="org.springframework.scheduling.quartz.SimpleTriggerBean">??
  • <property?name="jobDetail"?ref="job2"?/>??
  • <property?name="startDelay"?value="0"?/><!--?調(diào)度工廠實(shí)例化后,經(jīng)過(guò)0秒開(kāi)始執(zhí)行調(diào)度?-->??
  • <property?name="repeatInterval"?value="2000"?/><!--?每2秒調(diào)度一次?-->??
  • </bean>??
  • ?第二種CronTriggerBean,支持到指定時(shí)間運(yùn)行一次,如每天12:00運(yùn)行一次等。

    配置方式如下:

    Xml代碼 ?
  • <bean?id="cronTrigger"?class="org.springframework.scheduling.quartz.CronTriggerBean">??
  • <property?name="jobDetail"?ref="job2"?/>??
  • <!—每天12:00運(yùn)行一次?-->??
  • <property?name="cronExpression"?value="0?0?12?*?*??"?/>??
  • </bean>??
  • 以上兩種調(diào)度方式根據(jù)實(shí)際情況,任選一種即可。

    第四步:配置調(diào)度工廠?

    Xml代碼 ?
  • <bean?class="org.springframework.scheduling.quartz.SchedulerFactoryBean">??
  • <property?name="triggers">??
  • <list>??
  • <ref?bean="cronTrigger"?/>??
  • </list>??
  • </property>??
  • </bean>??
  • 說(shuō)明:該參數(shù)指定的就是之前配置的觸發(fā)器的名字。

    第五步:啟動(dòng)你的應(yīng)用即可,即將工程部署至tomcat或其他容器。

    ?

    到此,spring中Quartz的基本配置就介紹完了,當(dāng)然了,使用之前,要導(dǎo)入相應(yīng)的spring的包與Quartz的包,這些就不消多說(shuō)了。

    其實(shí)可以看出Quartz的配置看上去還是挺復(fù)雜的,沒(méi)有辦法,因?yàn)镼uartz其實(shí)是個(gè)重量級(jí)的工具,如果我們只是想簡(jiǎn)單的執(zhí)行幾個(gè)簡(jiǎn)單的定時(shí)任務(wù),有沒(méi)有更簡(jiǎn)單的工具,有!

    請(qǐng)看我第下文Spring task的介紹。

    ?

    Spring-Task

    上節(jié)介紹了在Spring 中使用Quartz,本文介紹Spring3.0以后自主開(kāi)發(fā)的定時(shí)任務(wù)工具,spring task,可以將它比作一個(gè)輕量級(jí)的Quartz,而且使用起來(lái)很簡(jiǎn)單,除spring相關(guān)的包外不需要額外的包,而且支持注解和配置文件兩種

    形式,下面將分別介紹這兩種方式。

    第一種:配置文件方式

    第一步:編寫(xiě)作業(yè)類

    即普通的pojo,如下:

    Java代碼 ?
  • import?org.springframework.stereotype.Service;??
  • @Service ??
  • public?class?TaskJob?{??
  • ??????
  • ????public?void?job1()?{??
  • ????????System.out.println(“任務(wù)進(jìn)行中。。。”);??
  • ????}??
  • }??
  • ?第二步:在spring配置文件頭中添加命名空間及描述

    Xml代碼 ?
  • <beans?xmlns="http://www.springframework.org/schema/beans"??
  • ????xmlns:task="http://www.springframework.org/schema/task"???
  • ????。。。。。。??
  • ????xsi:schemaLocation="http://www.springframework.org/schema/task?http://www.springframework.org/schema/task/spring-task-3.0.xsd">??
  • ?第三步:spring配置文件中設(shè)置具體的任務(wù)

    Xml代碼 ?
  • ?<task:scheduled-tasks>???
  • ????????<task:scheduled?ref="taskJob"?method="job1"?cron="0?*?*?*?*??"/>???
  • </task:scheduled-tasks>??
  • ??
  • <context:component-scan?base-package="?com.gy.mytask?"?/>??
  • 說(shuō)明:ref參數(shù)指定的即任務(wù)類,method指定的即需要運(yùn)行的方法,cron及cronExpression表達(dá)式,具體寫(xiě)法這里不介紹了,詳情見(jiàn)上篇文章附錄。

    <context:component-scan base-package="com.gy.mytask" />這個(gè)配置不消多說(shuō)了,spring掃描注解用的。

    到這里配置就完成了,是不是很簡(jiǎn)單。

    第二種:使用注解形式

    也許我們不想每寫(xiě)一個(gè)任務(wù)類還要在xml文件中配置下,我們可以使用注解@Scheduled,我們看看源文件中該注解的定義:

    Java代碼 ?
  • @Target ({java.lang.annotation.ElementType.METHOD,?java.lang.annotation.ElementType.ANNOTATION_TYPE})??
  • @Retention(RetentionPolicy.RUNTIME)??
  • @Documented??
  • public?@interface?Scheduled??
  • {??
  • ??public?abstract?String?cron();??
  • ??
  • ??public?abstract?long?fixedDelay();??
  • ??
  • ??public?abstract?long?fixedRate();??
  • }??
  • ?可以看出該注解有三個(gè)方法或者叫參數(shù),分別表示的意思是:

    cron:指定cron表達(dá)式

    fixedDelay:官方文檔解釋:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示從上一個(gè)任務(wù)完成開(kāi)始到下一個(gè)任務(wù)開(kāi)始的間隔,單位是毫秒。

    fixedRate:官方文檔解釋:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即從上一個(gè)任務(wù)開(kāi)始到下一個(gè)任務(wù)開(kāi)始的間隔,單位是毫秒。

    ?

    下面我來(lái)配置一下。

    第一步:編寫(xiě)pojo

    Java代碼 ?
  • import?org.springframework.scheduling.annotation.Scheduled;????
  • import?org.springframework.stereotype.Component;??
  • ??
  • @Component(“taskJob”)??
  • public?class?TaskJob?{??
  • ????@Scheduled(cron?=?"0?0?3?*?*??")??
  • ????public?void?job1()?{??
  • ????????System.out.println(“任務(wù)進(jìn)行中。。。”);??
  • ????}??
  • }??
  • ?第二步:添加task相關(guān)的配置:

    Xml代碼 ?
  • <?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:context="http://www.springframework.org/schema/context"??
  • ????xmlns:tx="http://www.springframework.org/schema/tx"??
  • ????xmlns:task="http://www.springframework.org/schema/task"??
  • ????xsi:schemaLocation="??
  • ????????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-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/jdbc/spring-jdbc-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/task?http://www.springframework.org/schema/task/spring-task-3.0.xsd"??
  • ????default-lazy-init="false">??
  • ??
  • ??
  • ????<context:annotation-config?/>??
  • ????<!—spring掃描注解的配置???-->??
  • ????<context:component-scan?base-package="com.gy.mytask"?/>??
  • ??????
  • <!—開(kāi)啟這個(gè)配置,spring才能識(shí)別@Scheduled注解???-->??
  • ????<task:annotation-driven?scheduler="qbScheduler"?mode="proxy"/>??
  • ????<task:scheduler?id="qbScheduler"?pool-size="10"/>??
  • 說(shuō)明:理論上只需要加上<task:annotation-driven />這句配置就可以了,這些參數(shù)都不是必須的。

    ?

    ?Ok配置完畢,當(dāng)然spring task還有很多參數(shù),我就不一一解釋了,具體參考xsd文檔http://www.springframework.org/schema/task/spring-task-3.0.xsd。

    附錄:

    cronExpression的配置說(shuō)明,具體使用以及參數(shù)請(qǐng)百度google

    字段 ? 允許值 ? 允許的特殊字符

    秒 ? ?0-59 ? ?, - * /

    分 ? ?0-59 ? ?, - * /

    小時(shí) ? ?0-23 ? ?, - * /

    日期 ? ?1-31 ? ?, - * ? / L W C

    月份 ? ?1-12 或者 JAN-DEC ? ?, - * /

    星期 ? ?1-7 或者 SUN-SAT ? ?, - * ? / L C #

    年(可選) ? ?留空, 1970-2099 ? ?, - * /?

    - 區(qū)間 ?

    * 通配符 ?

    ? 你不想設(shè)置那個(gè)字段

    下面只例出幾個(gè)式子

    ?

    CRON表達(dá)式 ? ?含義?

    "0 0 12 * * ?" ? ?每天中午十二點(diǎn)觸發(fā)?

    "0 15 10 ? * *" ? ?每天早上10:15觸發(fā)?

    "0 15 10 * * ?" ? ?每天早上10:15觸發(fā)?

    "0 15 10 * * ? *" ? ?每天早上10:15觸發(fā)?

    "0 15 10 * * ? 2005" ? ?2005年的每天早上10:15觸發(fā)?

    "0 * 14 * * ?" ? ?每天從下午2點(diǎn)開(kāi)始到2點(diǎn)59分每分鐘一次觸發(fā)?

    "0 0/5 14 * * ?" ? ?每天從下午2點(diǎn)開(kāi)始到2:55分結(jié)束每5分鐘一次觸發(fā)?

    "0 0/5 14,18 * * ?" ? ?每天的下午2點(diǎn)至2:55和6點(diǎn)至6點(diǎn)55分兩個(gè)時(shí)間段內(nèi)每5分鐘一次觸發(fā)?

    "0 0-5 14 * * ?" ? ?每天14:00至14:05每分鐘一次觸發(fā)?

    "0 10,44 14 ? 3 WED" ? ?三月的每周三的14:10和14:44觸發(fā)?

    "0 15 10 ? * MON-FRI" ? ?每個(gè)周一、周二、周三、周四、周五的10:15觸發(fā)?

    轉(zhuǎn)載于:https://my.oschina.net/MiniBu/blog/552426

    總結(jié)

    以上是生活随笔為你收集整理的Spring定时任务的几种实现方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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