一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Task定時任務的配置和使用詳解

Spring Task定時任務的配置和使用詳解

2020-09-07 09:16Joepis Java教程

本篇文章主要介紹了Spring Task定時任務的配置和使用詳解,實例分析了Spring Task定時任務的配置和使用的技巧,非常具有實用價值,需要的朋友可以參考下

記錄下Spring自帶的定時任務用法。

spring中使用定時任務

基于xml配置文件使用定時任務

首先配置spring開啟定時任務

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
 
  <task:annotation-driven /> <!-- 定時器開關-->
 
  <bean id="myTask" class="com.spring.task.MyTask"></bean>
 
  <task:scheduled-tasks>
    <!-- 這里表示的是每隔五秒執行一次 -->
    <task:scheduled ref="myTask" method="show" cron="*/5 * * * * ?" />
    <task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/>
  </task:scheduled-tasks>
 
  <!-- 自動掃描的包名 -->
  <context:component-scan base-package="com.spring.task" />
 
</beans>

定義自己的任務執行邏輯

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.spring.task;
 
/**
 * 定義任務
 */
public class MyTask {
 
  public void show() {
    System.out.println("show method 1");
  }
 
  public void print() {
    System.out.println("print method 1");
  }
}

基于注解使用定時任務

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.spring.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
/**
 * 基于注解的定時器
 */
@Component
public class MyTask2 {
 
  /**
   * 定時計算。每天凌晨 01:00 執行一次
   */
  @Scheduled(cron = "0 0 1 * * *")
  public void show() {
    System.out.println("show method 2");
  }
 
  /**
   * 啟動時執行一次,之后每隔2秒執行一次
   */
  @Scheduled(fixedRate = 1000*2
  public void print() {
    System.out.println("print method 2");
  }
}

這樣,當項目啟動,定時任務就會按照規則按時執行了。

Spring Boot中使用定時任務

Spring Boot中使用更加方便。

引入springboot starter

?
1
2
3
4
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
</dependency>

在程序入口啟動類添加@EnableScheduling,開啟定時任務功能

?
1
2
3
4
5
6
7
@SpringBootApplication
@EnableScheduling
public class Application {
 
 public static void main(String[] args) {
   SpringApplication.run(Application.class, args);
 }

定義定時任務邏輯

?
1
2
3
4
5
6
7
8
9
10
@Component
public class MyTask3 {
 
 private int count=0;
 
 @Scheduled(cron="*/6 * * * * ?")
 private void process() {
   System.out.println("this is scheduler task runing "+(count++));
 }
}

任務執行規則說明

先來看看@Scheduled注解的源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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 -1;
 
  String fixedDelayString() default "";
 
  long fixedRate() default -1;
 
  String fixedRateString() default "";
 
  long initialDelay() default -1;
 
  String initialDelayString() default "";
}

可以看出,注解中可以傳8種參數:

  1. cron:指定cron表達式
  2. zone:默認使用服務器默認時區。可以設置為java.util.TimeZone中的zoneId
  3. fixedDelay:從上一個任務完成開始到下一個任務開始的間隔,單位毫秒
  4. fixedDelayString:同上,時間值是String類型
  5. fixedRate:從上一個任務開始到下一個任務開始的間隔,單位毫秒
  6. fixedRateString:同上,時間值是String類型
  7. initialDelay:任務首次執行延遲的時間,單位毫秒
  8. initialDelayString:同上,時間值是String類型

cron表達式的使用方法

Cron表達式是一個字符串,字符串以5或6個空格隔開,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:

  1. Seconds Minutes Hours DayofMonth Month DayofWeek Year
  2. Seconds Minutes Hours DayofMonth Month DayofWeek

每一個域可出現的字符如下:

  1. Seconds: 可出現", - * /"四個字符,有效范圍為0-59的整數
  2. Minutes: 可出現", - * /"四個字符,有效范圍為0-59的整數
  3. Hours: 可出現", - * /"四個字符,有效范圍為0-23的整數
  4. DayofMonth: 可出現", - * / ? L W C"八個字符,有效范圍為0-31的整數
  5. Month: 可出現", - * /"四個字符,有效范圍為1-12的整數或JAN-DEC
  6. DayofWeek: 可出現", - * / ? L C #"四個字符,有效范圍為1-7的整數或SUN-SAT兩個范圍。1表示星期天,2表示星期一, 依次類推
  7. Year: 可出現", - * /"四個字符,有效范圍為1970-2099年

每一個域都使用數字,但還可以出現如下特殊字符,它們的含義是:

  1. *:表示匹配該域的任意值,假如在Minutes域使用*, 即表示每分鐘都會觸發事件。
  2. ?:只能用在DayofMonth和DayofWeek兩個域。它也匹配域的任意值,但實際不會。因為DayofMonth和 DayofWeek會相互影響。例如想在每月的20日觸發調度,不管20日到底是星期幾,則只能使用如下寫法: 13 13 15 20 ?, 其中最后一位只能用?,而不能使用,如果使用*表示不管星期幾都會觸發,實際上并不是這樣。
  3. -:表示范圍,例如在Minutes域使用5-20,表示從5分到20分鐘每分鐘觸發一次。
  4. /:表示起始時間開始觸發,然后每隔固定時間觸發一次,例如在Minutes域使用5/20,則意味著5分鐘觸發一次,而25,45等分別觸發一次。
  5. ,:表示列出枚舉值值。例如:在Minutes域使用5,20,則意味著在5和20分每分鐘觸發一次。
  6. L:表示最后,只能出現在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味著在最后的一個星期四觸發。
  7. W:表示有效工作日(周一到周五),只能出現在DayofMonth域,系統將在離指定日期的最近的有效工作日觸發事件。例如:在 DayofMonth使用5W,如果5日是星期六,則將在最近的工作日:星期五,即4日觸發。如果5日是星期天,則在6日(周一)觸發;如果5日在星期一 到星期五中的一天,則就在5日觸發。另外一點,W的最近尋找不會跨過月份。
  8. LW:這兩個字符可以連用,表示在某個月最后一個工作日,即最后一個星期五。
  9. #:用于確定每個月第幾個星期幾,只能出現在DayofMonth域。例如在4#2,表示某月的第二個星期三。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.jianshu.com/p/25c601f43552#

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产二区三区 | 无套大战白嫩乌克兰美女 | 久久国产精品高清一区二区三区 | 午夜看片a福利在线观看 | 精品国产一区二区在线观看 | 久久r视频 | caoporn草棚在线视频 | 日本videossexx日本人 | 欧美日韩国产在线人成 | 黑人与老女人做受 | 交换性关系中文字幕6 | 91精品啪在线观看国产老湿机 | 苍井空50分钟无码 | 国产亚洲欧美日韩综合综合二区 | 免费人成黄页在线观看69 | 国产一区二区免费视频 | 国产区成人综合色在线 | 亚洲欧美午夜 | 满城尽带黄金甲大胸片 | 亚洲国产精品久久网午夜 | 女八把屁股扒开让男生添 | 久热这里在线精品 | 精品国产欧美一区二区三区成人 | 国产精品原创巨作无遮挡 | 国产精品一区二区不卡的视频 | 99精彩视频在线观看 | 日本精品人妖shemale人妖 | 麻豆视频入口 | 国产白白视频在线观看2 | 无码AV免费精品一区二区三区 | 无人区大片免费播放器 | 亚洲国产婷婷俺也色综合 | 乳环贵妇堕落开发调教番号 | 乌克兰17一18处交 | 成 人 亚洲 综合天堂 | 日韩一区二区三区四区五区 | 26uuu成人人网图片 | 农村美女沟厕嘘嘘被偷看 | 吻戏辣妞范1000免费体验 | 无敌秦墨漫画免费阅读 | 亚洲AV蜜桃永久无码精品红樱桃 |