當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring的@Scheduled注解实现定时任务
生活随笔
收集整理的這篇文章主要介紹了
Spring的@Scheduled注解实现定时任务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring的@Scheduled注解實現定時任務
【簡介篇】
項目經常會用到定時任務,實現定時任務的方式有很多種。在Spring框架中,實現定時任務很簡單,常用的實現方式是使用注解@Scheduled。
@Scheduled 常用來實現簡單的定時任務。例如凌晨1點跑批,每10秒查詢支付狀態等
【實戰篇】
SpringBoot項目
1、配置
在spring boot的啟動類上加@EnableScheduling注解,允許支持@Scheduled:
@SpringBootApplication @EnableScheduling public class Application {public static void main(String[] args) throws Exception {SpringApplication.run(Application.class);} }2、任務類
@Component public class ScheduleTask {// 每隔5秒執行一次@Scheduled(cron = "0/5 * * * * ?")public void printSay() {System.out.println("每隔5秒執行一次:" + new Date());} }3、結果
Spring項目
1、配置
在spring.xml(或applicationContext.xml,以自己的配置文件名為準)中添加配置
<!-- 1、在 xmlns中加入 --> xmlns:task="http://www.springframework.org/schema/task"<!-- 2、 在xsi:schemaLocation中加入 -->http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd"<!— 3、 配置spring掃描注解的路徑 --> <context:component-scan base-package="com.it.mytask" /><!-- 4、 定時任務掃描配置 --> <!-- 4.1、 方式一: --> <task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven executor="executor" scheduler="scheduler" /><!-- 4.2、 方式二:--> <task:annotation-driven scheduler="scheduler" mode="proxy"/> <task:scheduler id="scheduler" pool-size="10"/>例
<?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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://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.it.mytask" /><!-- 定時任務掃描配置 --><!— 開啟這個配置,spring才能識別@Scheduled注解 --><task:annotation-driven scheduler="scheduler" mode="proxy"/><task:scheduler id="scheduler" pool-size="10"/></beans>2、任務類同上
3、結果同上
【原理篇】
@Scheduled源碼
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(Schedules.class) public @interface Scheduled {String cron() default "";String zone() default "";long fixedDelay() default -1L;String fixedDelayString() default "";long fixedRate() default -1L;String fixedRateString() default "";long initialDelay() default -1L;String initialDelayString() default ""; }案例
package com.task;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;@Component public class ScheduledTask {private Logger logger= LoggerFactory.getLogger(ScheduledTask.class);// fixedRate = 5000表示每隔5秒,Spring scheduling會調用一次該方法,不論該方法的執行時間是多少@Scheduled(fixedRate = 5000)public void task() {logger.info("每隔5秒執行一次");}// fixedDelay = 5000表示當方法執行完畢5秒后,Spring scheduling會再次調用該方法@Scheduled(fixedDelay = 5000)public void taskAfter() {logger.info("當方法執行完畢5秒后執行");}// cron = "*/5 * * * * * *" 通用的定時任務表達式,表示每隔5秒執行一次@Scheduled(cron = "*/5 * * * * *")public void taskCron() {logger.info("每隔5秒執行一次");} }cron 表達式
1、cron一共有7位,但是最后一位是年(1970-2099),可以留空,所以我們可以寫6位,按順序依次為
- 秒(0~59)
- 分鐘(0~59)
- 小時(0~23)
- 天(月)(1~31,但是你需要考慮你月的天數)
- 月(1~12)
- 星期(1~7 1=SUN,MON,TUE,WED,THU,FRI,SAT)
2、cron的一些特殊符號
- (*)星號:可以理解為每的意思,每秒,每分,每天,每月,每年…
- (?)問號:問號只能出現在日期和星期這兩個位置,表示這個位置的值不確定,每天3點執行,所以第六位星期的位置,我們是不需要關注的,就是不確定的值。同時:日期和星期是兩個相互排斥的元素,通過問號來表明不指定值。比如,1月10日,比如是星期1,如果在星期的位置是另指定星期二,就前后沖突矛盾了。
- (-)減號:表達一個范圍,如在小時字段中使用“10-12”,則表示從10到12點,即10,11,12
- (,)逗號:表達一個列表值,如在星期字段中使用“1,2,4”,則表示星期一,星期二,星期四
- (/)斜杠:如:x/y,x是開始值,y是步長,比如在第一位(秒) 0/15就是,從0秒開始,每15秒,最后就是0,15,30,45,60 另:*/y,等同于0/y
eg.下面列舉幾個例子供大家來驗證:
0 0 3 * * ? 每天3點執行 0 5 3 * * ? 每天3點5分執行 0 5 3 ? * * 每天3點5分執行,與上面作用相同 0 5/10 3 * * ? 每天3點的 5分,15分,25分,35分,45分,55分這幾個時間點執行 0 10 3 ? * 1 每周星期天,3點10分 執行,注:1表示星期天 0 10 3 ? * 1#3 每個月的第三個星期,星期天 執行,#號只能出現在星期的位置常用示例:
格式: [秒] [分] [小時] [日] [月] [周] [年]
總結
以上是生活随笔為你收集整理的Spring的@Scheduled注解实现定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Linux上编写并运行Java文件
- 下一篇: 在Linux上编写并运行Python文件