SpringBoot 集成 Quartz
假定为在单机场景下使用,且为经典的 SpringBoot 项目(用到 SpringBoot、Spring、SpringMVC、MyBatis 等框架)
引入依赖
在 pom.xml 中添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
执行 SQL
通常会需要持久化任务信息,对于 MySQL,在org.quartz-scheduler:quartz:2.3.2\quartz-2.3.2.jar\org\quartz\impl\jdbcjobstore
下找到tables_mysql_innodb.sql
并执行即可。
配置文件
在 application.yml 中添加配置:
spring: quartz: job-store-type: jdbc # Job 存储器类型。默认为 memory,表示内存,jdbc 表示使用数据库 scheduler-name: scheduler wait-for-jobs-to-complete-on-shutdown: true # 应用关闭时,是否等待定时任务执行完成。默认为 false,建议设置为 true overwrite-existing-jobs: false # 是否覆盖已有 Job 的配置 jdbc: initialize-schema: never # 数据库初始化方式,默认为 never,表示不初始化(前面已经手动初始化了) properties: # 添加 Quartz Scheduler 附加属性 org: quartz: scheduler: instanceName: scheduler instanceId: AUTO # 数据源配置 jobStore: class: org.quartz.impl.jdbcjobstore.JobStoreTX driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate tablePrefix: QRTZ_ isClustered: false clusterCheckinInterval: 10000 useProperties: false dataSource: quartzDs # 线程池配置 threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true
创建定时任务
这里直接封装一个常用方法到 Service 中,方便调用:
public interface ScheduleService { /** * 创建定时任务 * * @param jobClass 实现 Job 接口的类 * @param id 唯一标识 * @param cronExpression cron 表达式 */ void createIfNotExists(Class<? extends Job> jobClass, String id, String cronExpression) throws SchedulerException; }
@Slf4j @Service @RequiredArgsConstructor public class ScheduleServiceImpl implements ScheduleService { @Override public void createIfNotExists(Class<? extends Job> jobClass, String id, String cronExpression) throws SchedulerException { // 创建 Job JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity(id) .storeDurably() .build(); // 创建 Trigger Trigger trigger = TriggerBuilder.newTrigger() .forJob(jobDetail) .withIdentity(id) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)) .build(); // 判断 Trigger 是否存在 TriggerKey triggerKey = TriggerKey.triggerKey(trigger.getKey().getName(), trigger.getKey().getGroup()); if (scheduler.checkExists(triggerKey)) { return; } // 添加 Job 和 Trigger scheduler.scheduleJob(jobDetail, trigger); } }
之后创建 Job 实现类:
import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("任务正在执行..."); } }
在 Controller 中调用进行测试:
@RestController public class ScheduleController { @Autowired private ScheduleService scheduleService; @GetMapping("/schedule") public String schedule() throws SchedulerException { scheduleService.createIfNotExists(MyJob.class, "myJob", "0/5 * * * * ?"); } }
参考:SpringBoot 整合任务调度框架 Quartz 及持久化配置、SpringBoot-集成 Quartz 实现持久化定时接口调用任务
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库