SpringBoot整合定时任务和异步任务
SpringBoot定时任务schedule讲解
简介:讲解什么是定时任务和常见的定时任务区别
什么是定时任务,使用场景
- 某个时间定时处理某个任务
- 发邮件、短信等
- 消息提醒
- 订单通知
- 统计订单报表系统
- …
常见定时任务
- java自带的Java.util.Timer类配置比较麻烦,时间延后问题
- Quartz框架:配置简单,xml或者注解适合分布式或着大型调度作业
- SpringBoot自带框架
SpringBoot使用注解方式开启定时任务
- 启动类里面
@EnableScheduling
开启定时任务,自动扫描 - 定时任务业务类加注解
@Component
被容器扫描 - 定时执行的方法加上注解
@Scheduled
(fixedRate=2000)定期执行一次
SpringBoot多种定时任务配置示例
SpringBoot常用定时任务表达式配置和在线生成器
在线生成器和基本信息
- cron定时任务表达式
@Scheduled(cron="*/1 * * * * *")
表示每秒- crontab 工具 https://tool.lu/crontab/
- fixedRate:定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
- fixedDelay:上一次执行结束时间点后xx秒再次执行
示例
package net.xdclass.demoproject.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Random;
/**
* 定时统计订单,金额
*/
@Component
class VideoOrderTask {
//每2秒执行一次
@Scheduled(fixedDelay = 4000)
//@Scheduled(fixedRate = 4000)
//@Scheduled(cron = "*/1 * * * * *")
public void sum(){
//正常的是从数据库中查询
System.out.println(LocalDateTime.now() + " 当前交易额="+ Math.random());
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package net.xdclass.demoproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@ServletComponentScan
@EnableScheduling//开启定时任务
public class DemoProjectApplication {
public static void main(String[] args) {
SpringApplication.run(DemoProjectApplication.class, args);
}
}
SpringBoot异步任务EnableAsync实例
简介:讲解什么是异步任务,和使用SpringBoot开发异步任务实战
什么是异步任务和使用场景
- 适用于处理log、发送邮件、短信…等
- 下单接口->查库存1000
- 余额校验1500
- 风控用户1000
- 启动类里面使用
@EnableAsync
注解开启功能,自动扫描 - 定义异步任务类并使用
@Component
标记组件被容器扫描,异步方法加上@Async
SpringBoot异步任务Future实例
简介:使用SpringBoot开发异步任务Future获取结果
定义异步任务类需要获取结果
- 注意点:
- 要把异步任务封装到类里面,不能直接写到Controller
- 增加Future返回结果AsyncResult(“task执行完成”)
- 如果需要拿到结果需要判断全部的task.isDone()
实例
-
开启异步任务
-
关闭异步任务
package net.xdclass.demoproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@ServletComponentScan
@EnableScheduling
@EnableAsync//开启异步
public class DemoProjectApplication {
public static void main(String[] args) {
SpringApplication.run(DemoProjectApplication.class, args);
}
}
package net.xdclass.demoproject.task;
import net.xdclass.demoproject.domain.User;
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 AsyncTask {
public void task1(){
try {
Thread.sleep(4000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" task 1 ");
}
public void task2(){
try {
Thread.sleep(4000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" task 2 ");
}
public void task3(){
try {
Thread.sleep(4000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" task 3 ");
}
public Future<String> task4(){
try {
Thread.sleep(4000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" task 4 ");
return new AsyncResult<String>("task4");
}
public Future<String> task5(){
try {
Thread.sleep(4000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" task 5 ");
return new AsyncResult<String>("task5");
}
}