SpringBoot整合定时任务和异步任务处理,springboot异步
SpringBoot整合定时任务和异步任务处理,springboot异步
SpringBoot定时任务schedule讲解
1、常见定时任务 Java自带的java.util.Timer类
timer:配置比较麻烦,时间延后问题
timertask:不推荐
2、Quartz框架
配置更简单
xml或者注解
3、SpringBoot使用注解方式开启定时任务
1)启动类里面 @EnableScheduling开启定时任务,自动扫描
2)定时任务业务类 加注解 @Component被容器扫描
3)定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次
2、SpringBoot常用定时任务配置实战
简介:SpringBoot常用定时任务表达式配置和在线生成器
1、cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
1)crontab 工具 https://tool.lu/crontab/
Linux
* * * * * *
- - - - - -
| | | | | |
| | | | | + year [optional]
| | | | +----- day of week (0 - 7) (Sunday=0 or 7)
| | | +---------- month (1 - 12)
| | +--------------- day of month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)
Java(Spring)
* * * * * * *
- - - - - - -
| | | | | | |
| | | | | | + year [optional]
| | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
| | | | +---------- month (1 - 12)
| | | +--------------- day of month (1 - 31)
| | +-------------------- hour (0 - 23)
| +------------------------- min (0 - 59)
+------------------------------ second (0 - 59)
参考博客:https://www.cnblogs.com/mingyue1818/p/5764050.html
举几个例子:
0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务
0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
0 15 10 ? 6L 2002-2006 表示200-2006年的每个月的最后一个星期五上午10:15执行作业
2、fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
@Component
public class TestTask {
@Scheduled(fixedRateString="2000")
public void test() throws InterruptedException {
Thread.sleep(4000L);
System.out.println(new Date());
}
}
每4秒执行一次
3、fixedDelay: 上一次执行结束时间点后xx秒再次执行
@Component
public class TestTask {
@Scheduled(fixedDelay=2000)
public void test() throws InterruptedException {
Thread.sleep(4000L);
System.out.println(new Date());
}
}
每6秒执行一次
4、fixedDelayString: 字符串形式,可以通过配置文件指定
相关文章
- 暂无相关文章
用户点评