【SpringBoot】整合定時任務和異步任務

========================十、SpringBoot整合定時任務和異步任務處理 ===============================java

 

一、SpringBoot定時任務schedule講解
簡介:講解什麼是定時任務和常見定時任務區別web

一、常見定時任務 Java自帶的java.util.Timer類
timer:配置比較麻煩,時間延後問題
timertask:不推薦spring

二、Quartz框架
配置更簡單
xml或者註解api

三、SpringBoot使用註解方式開啓定時任務
1)啓動類裏面 @EnableScheduling開啓定時任務,自動掃描
2)定時任務業務類 加註解 @Component被容器掃描
3)定時執行的方法加上註解 @Scheduled(fixedRate=2000) 按期執行一次app

 

二、SpringBoot經常使用定時任務配置實戰
簡介:SpringBoot經常使用定時任務表達式配置和在線生成器框架

package net.xdclass.base_project.task;

import java.util.Date;

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

/**
 * 功能描述:定時任務業務類
 *
 * <p> 建立時間:Apr 30, 2018 10:21:48 AM </p> 
 *
 *@做者 小D課堂  小D
 */
@Component
public class TestTask {

    
    @Scheduled(fixedRate=2000) //兩秒執行一次
    public void sum(){
        System.out.println("當前時間:"+new Date());
    }
    
    
}
Task

控制檯dom

 

 

一、cron 定時任務表達式 @Scheduled(cron="*/1 * * * * *") 表示每秒
1)crontab 工具 https://tool.lu/crontab/
二、fixedRate: 定時多久執行一次(上一次開始執行時間點後xx秒再次執行;)
三、fixedDelay: 上一次執行結束時間點後xx秒再次執行
四、fixedDelayString: 字符串形式,能夠經過配置文件指定
異步

 流程:新建任務類註冊到context,經過@Scheduled註解自動執行async

package net.xdclass.base_project.task;

import java.util.Date;

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

/**
 * 功能描述:定時任務業務類
 *
 * <p> 建立時間:Apr 30, 2018 10:21:48 AM </p> 
 *
 *@做者 小D課堂  小D
 */
@Component
public class TestTask {

    
    @Scheduled(fixedRateString="2000")//兩秒執行一次
    //@Scheduled(cron="*/2 * * * * *")
    public void sum() throws InterruptedException{

        Thread.sleep(4000L);
        System.out.println("結束 當前時間:"+new Date());
        
    }
    
    
    
    
    //@Scheduled(cron="*/1 * * * * *")
    public void sum2(){
        System.out.println("cron 每秒 當前時間:"+new Date());
    }
    
    
    
    
    
}
task

 

三、SpringBoot2.x異步任務實戰(核心知識)
簡介:講解什麼是異步任務,和使用SpringBoot2.x開發異步任務實戰
一、什麼是異步任務和使用場景:適用於處理log、發送郵件、短信……等
下單接口->查庫存 100
餘額校驗 150
風控用戶100
....ide


二、啓動類裏面使用@EnableAsync註解開啓功能,自動掃描

三、定義異步任務類並使用@Component標記組件被容器掃描,異步方法加上@Async(能夠註解到類)

流程:新建任務類註冊到context,  controller成員屬性注入任務類,   成員方法中執行任務類方法
注意點:
1)要把異步任務封裝到類裏面,不能直接寫到Controller
2)增長Future<String> 返回結果 AsyncResult<String>("task執行完成");
3)若是須要拿到結果 須要判斷所有的 task.isDone()
四、經過注入方式,注入到controller裏面,若是測試先後區別則改成同步則把Async註釋掉

package net.xdclass.base_project.task;

import java.util.concurrent.Future;

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

/**
 * 功能描述:異步任務業務類
 *
 * <p> 建立時間:Apr 30, 2018 11:25:15 PM </p> 
 *
 *@做者 小D課堂  小D
 */
@Component
//@Async
public class AsyncTask {

    
    public void task1() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();
        System.out.println("任務1耗時="+(end-begin));
    }
    
    
    public void task2() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();
        System.out.println("任務2耗時="+(end-begin));
    }
    
    
    public void task3() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();
        System.out.println("任務3耗時="+(end-begin));
    }
    
    
    //獲取異步結果
    
    
    public Future<String> task4() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();
        System.out.println("任務4耗時="+(end-begin));
        return new AsyncResult<String>("任務4");
    }
    
    
    public Future<String> task5() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();
        System.out.println("任務5耗時="+(end-begin));
        return new AsyncResult<String>("任務5");
    }
    
    public Future<String> task6() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();
        System.out.println("任務6耗時="+(end-begin));
        return new AsyncResult<String>("任務6");
    }
    
    
    
    
    
}
AsynTask

 

 

 

package net.xdclass.base_project.controller;

import java.util.concurrent.Future;

import net.xdclass.base_project.domain.JsonData;
import net.xdclass.base_project.task.AsyncTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1")
public class UserController {

    
    @Autowired
    private AsyncTask task;
    
    @GetMapping("async_task")
    public JsonData exeTask() throws InterruptedException{
        
        long begin = System.currentTimeMillis();
        
//        task.task1();
//        task.task2();
//        task.task3();

        Future<String> task4 = task.task4();
        Future<String> task5 = task.task5();
        Future<String> task6 = task.task6();
        for(;;){
            if (task4.isDone() && task5.isDone() && task6.isDone()) {
                break;
            }
        }
        
        
        long end = System.currentTimeMillis();
        
        long total = end-begin;
        System.out.println("執行總耗時="+total);
        return JsonData.buildSuccess(total);
    }
    
    
}
controller

異步執行,拿回結果,

相關文章
相關標籤/搜索