【Spring Boot學習之六】Spring Boot整合定時任務&異步調用


環境
  eclipse 4.7
  jdk 1.8
  Spring Boot 1.5.2
一、定時任務
1、啟動類添加注解@EnableScheduling 用於開啟定時任務

package com.wjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling //開啟定時任務
public class APP {

    public static void main(String[] args) {
        SpringApplication.run(APP.class, args);
    }

}

2、定義@Component定時組件類和@Scheduled定義執行周期

/**
 * 
 */
package com.wjy.scheduled;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Desc 定時任務
 * @author wangjy15
 */
@Component
public class ScheduledTasks {
    
    //fixedRate 周期 頻率
    //失敗后一直重試
    //@Scheduled(fixedRate=1000)
    @Scheduled(cron="0/1 * * * * ?")
    public void task() {
        System.out.println("每隔1秒鍾打印...");
//        每隔1秒鍾打印...
//        每隔1秒鍾打印...
//        每隔1秒鍾打印...
//        每隔1秒鍾打印...
//        每隔1秒鍾打印...
    }

}

兩種方式:

(1)fixedDelay:
示例:@Scheduled(fixedRate=1000) 每隔1秒
(2)cron表達式:
示例:@Scheduled(cron = "0/10 * * * * ?")每隔1秒
Seconds Minutes Hours DayofMonth Month DayofWeek,注意:spring boot不支持年
每一個域可出現的字符如下:

Seconds: 可出現", - * /"四個字符,有效范圍為0-59的整數 
Minutes: 可出現", - * /"四個字符,有效范圍為0-59的整數 
Hours: 可出現", - * /"四個字符,有效范圍為0-23的整數 
DayofMonth :可出現", - * / ? L W C"八個字符,有效范圍為0-31的整數 
Month: 可出現", - * /"四個字符,有效范圍為1-12的整數或JAN-DEc 
DayofWeek: 可出現", - * / ? L C #"四個字符,有效范圍為1-7的整數或SUN-SAT兩個范圍。1表示星期天,2表示星期一, 依次類推 
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,表示某月的第二個星期三。
舉幾個例子:
每隔5秒執行一次:"*/5 * * * * ?"
每隔1分鍾執行一次:"0 */1 * * * ?"
每天23點執行一次:"0 0 23 * * ?"
每天凌晨1點執行一次:"0 0 1 * * ?"
每月1號凌晨1點執行一次:"0 0 1 1 * ?"
每月最后一天23點執行一次:"0 0 23 L * ?"
每周星期天凌晨1點實行一次:"0 0 1 ? * L"
在26分、29分、33分執行一次:"0 26,29,33 * * * ?"
每天的0點、13點、18點、21點都執行一次:"0 0 0,13,18,21 * * ?"
表示在每月的1日的凌晨2點調度任務:"0 0 2 1 * ? *"
表示周一到周五每天上午10:15執行作業:"0 15 10 ? * MON-FRI"
表示2002-2006年的每個月的最后一個星期五上午10:15執行:"0 15 10 ? 6L 2002-2006"
注意:由於"月份中的日期"和"星期中的日期"這兩個元素互斥的,必須要對其中一個設置?
cron表達式 參考:http://cron.qqe2.com/

3、Spring Boot整合Quzrtz

二、Spring Boot異步執行

類似開啟了一個線程來執行,使用場景:發送短信、發送郵件、APP消息推送

1、添加@EnableAsync 開啟異步

package com.wjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync public class APP {

    public static void main(String[] args) {
        SpringApplication.run(APP.class, args);
    }

}

2、定義@Component類和@Async方法

package com.wjy.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class AsyncTask {
     
    @Async public String sendSms() {
        System.out.println("###sendSms###3");
        System.out.println("sendSms" );
        System.out.println("###sendSms###4");
        return "success";
    }

}

3、controller

package com.wjy.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wjy.async.AsyncTask;

@RestController
public class UserController {

    private static Logger log = Logger.getLogger(UserController.class);
    
    @RequestMapping("/sendSms")
    public String sendSms() {
        System.out.println("###sendMsg###1");
        asyncTask.sendSms();
        System.out.println("###sendMsg###2");
        return "success";
    }
    
}

4、測試驗證:http://localhost:8080/sendSms

不添加注解@Async:

###sendMsg###1
###sendSms###3
sendSms
###sendSms###4
###sendMsg###2

添加注解@Async:

###sendMsg###1
###sendMsg###2
###sendSms###3
sendSms
###sendSms###4

 

參考:

springboot+quartz整合:
https://blog.csdn.net/tuesdayma/article/details/81538270
https://blog.csdn.net/tuesdayma/article/details/81563011
https://segmentfault.com/a/1190000016554033
https://www.jb51.net/article/148204.htm

Quartz簡介:
https://www.cnblogs.com/zhanghaoliang/p/7886110.html
Quartz表+表字段含義:
https://blog.csdn.net/fly_captain/article/details/83058147


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM