11.定时任务(Scheduled)

定时任务

文档

定时任务文档地址

1.实现方案

1.1.Timer


1.2.线程池


1.3.mq的延时队列


1.4.QUARTZ

1.搭配cron表达式使用
2.支持年,spring不支持年
3.在周几的位置,quartz的周1是2,spring的周1是1
4.

spring框架的定时任务

springboot默认定时任务框架不是QUARTZ,如果需要使用引入即可		

2.cron表达式

语法

语法:秒 分 时 日 月 周 年 (spring 不支持年,所以可以不写)
http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:

Field Name	Mandatory	Allowed Values		Allowed Special Characters
Seconds			YES		0-59				, - * /
Minutes			YES		0-59				, - * /
Hours			YES		0-23				, - * /
Day of month	YES		1-31				, - * ? / L W
Month			YES		1-12 or JAN-DEC		, - * /
Day of week		YES		1-7 or SUN-SAT		, - * ? / L #
                        1:周末 7:周一
Year			NO		empty, 1970-2099	, - * /

特殊字符:
,:枚举;
(cron="7,9,23****?"):任意时刻的7,923秒启动这个任务;
-:范围:
(cron="7-20****?""):任意时刻的7-20秒之间,每秒启动一次
*:任意;
指定位置的任意时刻都可以
/:步长;
(cron="7/5****?"):7秒启动,每5秒一次;
(cron="*/5****?"):任意秒启动,每5秒一次;

?:(出现在日和周几的位置):为了防止日和周冲突,在周和日上如果要写通配符使用?
(cron="***1*?"):每月的1号,而且必须是周二然后启动这个任务;

L:(出现在日和周的位置)”,
last:最后一个
(cron="***?*3L"):每月的最后一个周二(1:周末 2:周一 3:周二 4:周三 5:周四 6:周五 7:周六)

WWork Day:工作日
(cron="***W*?"):每个月的工作日触发
(cron="***LW*?"):每个月的最后一个工作日触发
#:第几个
(cron="***?*5#2"):每个月的 第2个周4

3.在线Cron表达式生成器

2.cron示例

**Expression**		**Meaning**
0 0 12 * * ?		Fire at 12pm (noon) every day
0 15 10 ? * *		Fire at 10:15am every day
0 15 10 * * ?		Fire at 10:15am every day
0 15 10 * * ? *		Fire at 10:15am every day
0 15 10 * * ? 2005	Fire at 10:15am every day during the year 2005
0 * 14 * * ?		Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ?		Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ?	Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ?		Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WED	Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRI	Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ?		Fire at 10:15am on the 15th day of every month
0 15 10 L * ?		Fire at 10:15am on the last day of every month
0 15 10 L-2 * ?		Fire at 10:15am on the 2nd-to-last last day of every month
0 15 10 ? * 6L		Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L		Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005	Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3		Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ?		Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ?		Fire every November 11th at 11:11am.

3.SpringBoot整合

开启定时任务

整合步骤:
1.@EnableScheduling【spring 默认是使用自己的定时任务,如果想整合Quartz,参考官方】
2.@Scheduled
3.定时任务配置类:TaskSchedulingAutoConfiguration
@Slf4j
@Component
@EnableScheduling
public class HelloScheduled {
    
     @Scheduled(cron = "*/5 * * ? * 4")// 周四的任意秒启动,每隔五秒执行一次
     public void hello() {
         log.info("hello...");
     }
    
}

定时任务+异步任务

定时任务默认情况排队执行,所以前一个任务超时后面任务也会超时

解决方案:
	方案1:业务方法自己作异步编排【CompletableFuture.runAsync】
	方案2:修改定时任务线程池的线程个数【spring.task.scheduling.pool.size=5】(不一定生效,有BUG)
	方案3:让定时任务异步执行
方案3:让定时任务异步执行
整合步骤:
1.@EnableAsync
2.@Async
3.异步配置类:TaskExecutionAutoConfiguration
    配置线程池:
    	# 核心线程数
    	spring.task.execution.pool.core-size=5
    	# 最大线程数
    	spring.task.execution.pool.max-size=50

@Slf4j
@Component
@EnableAsync// 普通service方法也可以标注异步执行
@EnableScheduling
public class HelloScheduled {
    
     @Async
     @Scheduled(cron = "*/5 * * ? * 4")// 周四的任意秒启动,每隔五秒执行一次
     public void hello() {
         log.info("hello...");
         try {
             TimeUnit.SECONDS.sleep(3);
         } catch (InterruptedException e) { 
             e.printStackTrace(); 
         }
     }
}

分布式定时任务

1.定时任务问题

2.扩展-分布式调度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值