作者:晴儿19082 | 来源:互联网 | 2023-05-17 10:35
本文主要分享【springbootapplication组合注解】,技术文章【springboot基础(45):注解方式整合quartz】为【逆水行舟没有退路】投稿,如果你遇到springboot,q
本文主要分享【springbootapplication组合注解】,技术文章【springboot基础(45):注解方式整合quartz】为【逆水行舟没有退路】投稿,如果你遇到springboot,quartz相关问题,本文相关知识或能到你。
springbootapplication组合注解
文章目录 前言使用注解的定时任务定时任务的配置cron扩展资料
前言
本文属于pringboot基础(44):quartz的快速开发版,建议先阅读前文。
quartz,用于处理一些定时任务相关的业务,支持cron表达式。
相关概念
工作Job: 用于定义具体执行的工作工作明细JobDetail: 用于描述定时工作相关的信息触发器Trigger: 用于描述触发工作的规则,一般使用cron表达式定义规则调度器Scheduler:描述了工作明细和触发器的对应关系
原始的方式去整合quartz实现定时功能业务,需要定义Job,JobDetail ,绑定Trigger,并添加调度器,用起来很繁琐。
本章介绍如何使用注解来实现定时任务。
使用注解的定时任务 导入依赖
<dependency>
<groupId>org.springframework.boot
groupId> <artifactId>spring-boot-starter-quartz
artifactId>
dependency>
开启定时任务功能,在主类上添加注解@EnableScheduling
@SpringBootApplication
@EnableScheduling
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
在定时任务的类上添加@Component,让spring将其加载到容器,在需要执行的定时方法上,添加@Scheduled设置执行时间,设置运行时间使用cron表达式。
@Slf4j
@Component
public class MyJob {
@Scheduled(cron = "0,15,30,45 * * * * ?")
public void hello(){
log.info("say hello");
}
}
启动服务器,服务器以设置的定时频率执行。
完成。
定时任务的配置
通常在生产环境下需要在application.yml配置scheduling的参数
spring:
task:
scheduling:
pool:
size: 1
thread-name-prefix: myquartz_
shutdown:
await-termination: false
await-termination-period: 10s
thread-name-prefix 线程名前缀
await-termination 服务关闭时是否等待所有任务都执行完成
await-termination-period : 等待若干时间后强制关闭,避免程序内的死循环导致任务一直无法关闭
cron扩展资料
阿里云cron表达式
详解cron表达式
在线Cron表达式生成器
本文《springboot基础(45):注解方式整合quartz》版权归逆水行舟没有退路所有,引用springboot基础(45):注解方式整合quartz需遵循CC 4.0 BY-SA版权协议。