springboot整合任务有很多种方法,下面以Quartz跟Task作为整合,快速把握。其中Task是比较常用以及我个人推荐,而且上手比较简单。
Task技术整合
spring根据定时任务的特征,将定时任务的开发简化到了极致。 在springboot项目中使用也是同样的道理。只要设置一个定时任务告诉容器有,然后定时执行什么任务直接告诉对应的bean什么时间执行就行了。
步骤①:开启定时任务功能,在引导类上开启定时任务功能的开关,使用注解@EnableScheduling
@SpringBootApplication //开启定时任务功能 @EnableScheduling public class TaskApplication { public static void main(String[] args) { TaskApplication .run(Springboot22TaskApplication.class, args); } }
步骤②:定义Bean,在对应要定时执行的操作上方,使用注解@Scheduled定义执行的时间,执行时间的描述方式还是cron表达式
@Component public class MyBean { @Scheduled(cron = "0/1 * * * * ?") public void print(){ System.out.println(Thread.currentThread().getName()+" :springboot task run..."); } }
这就以及完成了定时任务的配置,直接使用注解绑定定时执行任务。如何想对定时任务进行相关配置,可以通过配置文件进行
spring: task: scheduling: pool: size: 1 # 任务调度线程池大小 默认 1 thread-name-prefix: ssm_ # 调度线程名称前缀 默认 scheduling- shutdown: await-termination: false # 线程池关闭时等待所有任务完成 await-termination-period: 10s # 调度线程关闭前最大等待时间,确保最后一定关闭
Quartz技术整合
Quartz技术是一个比较成熟的定时任务框架但是配置略微复杂繁琐。
Quartz的常见的概念
- 工作(Job):用于定义具体执行的工作
- 工作明细(JobDetail):用于描述定时工作相关的信息
- 触发器(Trigger):描述了工作明细与调度器的对应关系
- 调度器(Scheduler):用于描述触发工作的执行规则,通常使用cron表达式定义规则
设置一个调度器,可以简单理解成设置一个工作执行的时间。工作和调度都是独立定义的它们两个通过触发器配合到一起。
步骤①:导入springboot整合Quartz的starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
步骤②:定义任务Bean,按照Quartz的开发规范制作,继承QuartzJobBean
public class MyQuartz extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("quartz task run..."); } }
步骤③:创建Quartz配置类,定义工作明细(JobDetail)与触发器的(Trigger)bean
/** * @author 风轻云淡 */ @Configuration public class QuartzConfig { @Bean public JobDetail printJobDetail(){ //绑定具体的工作 return JobBuilder.newJob(MyQuartz.class).storeDurably().build(); } @Bean public Trigger printJobTrigger(){ //对应的参数是cron表达式 ScheduleBuilder schedBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?"); //绑定对应的工作明细 return TriggerBuilder.newTrigger().forJob(printJobDetail()).withSchedule(schedBuilder).build(); } }
触发器需要绑定任务,使用forJob()操作传入绑定的工作明细对象。此处可以为工作明细设置名称然后使用名称绑定,也可以直接调用对应方法绑定。触发器中最核心的规则是执行时间,此处使用调度器定义执行时间,执行时间描述方式使用的是cron表达式。
cron表达式
cron 是一个已经存在了很长时间的 UNIX 工具,因此它的调度能力很强大 并得到证明。CronTrigger 类基于 cron 的调度功能。
CronTrigger 使用“cron 表达式”,它能够创建触发时间表,例如:“在上午 8:00 每个 周一至周五“或”每月最后一个星期五凌晨 1:30”。cron 表达式很强大,但可能会非常令人困惑。你可以使用这个工具直接进行转换
在线Cron表达式生成器 (qqe2.com)https://cron.qqe2.com/
1、cron 表达式
语法:秒 分 时 日 月 周 年(Spring 不支持)
特殊字符: ,:枚举;
(cron="7,9,23 * * * * ?"):任意时刻的 7,9,23 秒启动这个任务;
-:范围:
(cron="7-20 * * * * ?"):任意时刻的 7-20 秒之间,每秒启动一次
*:任意; 指定位置的任意时刻都可以
/:步长;
(cron="7/5 * * * * ?"):第 7 秒启动,每 5 秒一次;
(cron="*/5 * * * * ?"):任意秒启动,每 5 秒一次; ?:(出现在日和周几的位置):为了防止日和周冲突,在周和日上如果要写通配符使 用? (cron="* * * 1 * ?"):每月的 1 号,启动这个任务;
L:(出现在日和周的位置)”,
last:最后一个
(cron="* * * ? * 3L"):每月的最后一个周二
W:
Work Day:工作日
(cron="* * * W * ?"):每个月的工作日触发
(cron="* * * LW * ?"):每个月的最后一个工作日触发
#:第几个
(cron="* * * ? * 5#2"):每个月的第 2 个周 4
2、cron 示例
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. |