Spring Boot 整合定时任务和异步任务处理

1.定时任务

Spring Boot 使用注解方式开启定时任务,分为3步

1)启动类里面加上 @EnableScheduling 注解开启定时任务,自动扫描标记了@Scheduled 注解的方法

@SpringBootApplication
@EnableScheduling
public class BaseProjectApplication {

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

}

2)定时任务业务类加上 @Component 注解,用于被容器扫描

3)定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次

@Component
public class TestTask {
    
    @Scheduled(fixedRate=2000)//两秒执行一次
    public void sum() throws InterruptedException{
        System.out.println("结束 当前时间:"+new Date());

    }
}       

常用定时任务表达式配置

(1)fixedRate: 定时多久执行一次(上一次开始执行时间点后xx毫秒再次执行)

(2)fixedRateString: 定时多久执行一次(上一次开始执行时间点后xx毫秒再次执行),字符串形式,可以通过配置文件指定

(3)fixedDelay: 定时多久执行一次(上一次执行结束时间点后xx毫秒再次执行)

(4)fixedDelayString: 定时多久执行一次(上一次执行结束时间点后xx毫秒再次执行),字符串形式,可以通过配置文件指定

(5)cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒。crontab 工具 https://tool.lu/crontab/

@Scheduled(fixedRate=2000)//两秒执行一次
@Scheduled(fixedRateString="2000")//两秒执行一次

@Scheduled(fixedDelay=2000)//两秒执行一次
@Scheduled(fixedDelayString="2000")//两秒执行一次

@Scheduled(cron="*/2 * * * * *")//每两秒执行一次

2.异步任务

Spring Boot 使用注解方式开启异步任务,分为3步

1)启动类里面加上 @EnableAsync 注解开启异步任务,自动扫描标记了 @Async 注解的方法

@SpringBootApplication 
@EnableAsync   //开启异步任务
public class XdclassApplication {
    public static void main(String[] args) {
        SpringApplication.run(XdclassApplication.class, args);
    }
}

2)异步任务业务类加上 @Component 注解,用于被容器扫描

3)异步执行的方法加上注解 @Async 表示该方法可以异步执行,也可以在异步任务业务类加上 @Async 表示该类中的所有方法可以异步执行

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

测试代码如下:

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

注意点:

1)要把异步任务封装到类里面,不能直接写到 Controller

2)增加 Future<String>  返回结果 AsyncResult<String>("task执行完成"); 

3)如果需要拿到结果,需要判断全部的 task.isDone();

原文地址:https://www.cnblogs.com/jwen1994/p/11372615.html