SpringBoot定时任务schedule 异步任务实例(整理二)

1、定时器异步任务使用场景

使用场景:适用于处理log、发送邮件、短信……等

下单接口->
查库存 100
余额校验 150
风控用户100

2、启动类里面使用@EnableAsync注解开启异步功能,自动扫描

@SpringBootApplication //一个注解顶下面3个
@EnableScheduling	//开启定时任务
@EnableAsync   //开启异步任务
public class FbiaoApplication {
	public static void main(String[] args) {
		SpringApplication.run(FbiaoApplication.class, args);
	}
}

3、 @Async 异步任务处理

定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async
注意点:
1)要把异步任务封装到类里面,不能直接写到Controller
2)增加Future 返回结果 AsyncResult(“task执行完成”);
3)如果需要拿到结果 需要判断全部的 task.isDone()

(1)不需要返回异步处理结果
package com.mybatis.basepro.task;

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

import java.util.concurrent.Future;

@Component
@Async
public class TestAsyncTask {

    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));
    }
}
package com.mybatis.basepro.controller;

import com.mybatis.basepro.domain.JsonData;
import com.mybatis.basepro.task.TestAsyncTask;
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/task")
public class TestAsuncTaskController {
    @Autowired
    private TestAsyncTask task;
    @GetMapping("test_async_task")
    public JsonData testAsyncTask() throws InterruptedException{
        long begin = System.currentTimeMillis();
		task.task1();
		task.task2();
		task.task3();
        long end = System.currentTimeMillis();
        long total = end-begin;
        System.out.println("执行总耗时="+total);
        return JsonData.buildSuccess(total);
    }
}

SpringBoot定时任务schedule 异步任务实例(整理二)_第1张图片

(2)需要返回异步处理结果
package com.mybatis.basepro.task;

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

import java.util.concurrent.Future;

@Component
@Async
public class TestAsyncTask {


    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));
    }


    //获取异步结果
    
    //java.util.concurrent.Future; 这个包,并发编程Future
    
    public Future task4() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();
        System.out.println("任务4耗时="+(end-begin));
        
        
        //new AsyncResult实现了Future
        return new AsyncResult("任务4");
    }


    public Future task5() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();
        System.out.println("任务5耗时="+(end-begin));
        return new AsyncResult("任务5");
    }

    public Future task6() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();
        System.out.println("任务6耗时="+(end-begin));
        return new AsyncResult("任务6");
    }
}


package com.mybatis.basepro.controller;

import com.mybatis.basepro.domain.JsonData;
import com.mybatis.basepro.task.TestAsyncTask;
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;

import java.util.concurrent.Future;
@RestController
@RequestMapping("/api/task")
public class TestAsuncTaskController {
    @Autowired
    private TestAsyncTask task;

    @GetMapping("/test_async_task")
    public JsonData testAsyncTask() throws InterruptedException{

        long begin = System.currentTimeMillis();

//		task.task1();
//		task.task2();
//		task.task3();

        Future task4 = task.task4();
        Future task5 = task.task5();
        Future task6 = task.task6();
        for(;;){
        
            //task4.isDone() 执行完成 task4.cancel()任务取消
            
            if (task4.isDone() && task5.isDone() && task6.isDone()) {
                break;
            }
        }
        long end = System.currentTimeMillis();
        long total = end-begin;
        System.out.println("执行总耗时="+total);
        return JsonData.buildSuccess(total);
    }
}

SpringBoot定时任务schedule 异步任务实例(整理二)_第2张图片

(3)需要返回同步处理结果(把@Async注释掉)

在这里插入图片描述

(4)不需要返回同步处理结果(把@Async注释掉)

SpringBoot定时任务schedule 异步任务实例(整理二)_第3张图片

你可能感兴趣的:(后端)