SpringBoot 整合Quartz框架的简单实用
转载自https://blog.csdn.net/wk52525/article/details/79100973
1 maven依赖
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2 创建第一个任务
package top.kylewang.bos.quartz; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Component; @Component @EnableScheduling public class FirstJob{ public void task(){ System.out.println("任务1执行...."); } }
创建第二个任务
package top.kylewang.bos.quartz; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Component; @Component @EnableScheduling public class SecondJob{ public void task(){ System.out.println("任务2执行...."); } }
3创建触发器和开启任务
package top.kylewang.quartz; import org.quartz.JobDetail; import org.quartz.Trigger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; import org.springframework.scheduling.quartz.CornTriggerFactoryBean; import top.kylewang.bos.quartz.FirstJob ; import top.kylewang.bos.quartz.SecondJob; @Configuration public class QuartzConfiguration { // 配置定时任务1 @Bean(name = "firstJobDetail") public MethodInvokingJobDetailFactoryBean firstJobDetail(FirstJob firstJob) { MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean(); // 是否并发执行 jobDetail.setConcurrent(false); // 为需要执行的实体类对应的对象 jobDetail.setTargetObject(firstJob); // 需要执行的方法 jobDetail.setTargetMethod("task"); return jobDetail; } // 配置触发器1 @Bean(name = "firstTrigger") public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) { SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean(); trigger.setJobDetail(firstJobDetail); // 设置任务启动延迟 trigger.setStartDelay(0); // 每5秒执行一次 trigger.setRepeatInterval(5000); return trigger; } // 配置定时任务2 @Bean(name = "secondJobDetail") public MethodInvokingJobDetailFactoryBean secondJobDetail(SecondJob secondJob) { MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean(); // 是否并发执行 jobDetail.setConcurrent(false); // 为需要执行的实体类对应的对象 jobDetail.setTargetObject(secondJob); // 需要执行的方法 jobDetail.setTargetMethod("task"); return jobDetail; } // 配置触发器2 @Bean(name = "secondTrigger") public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail) { CronTriggerFactoryBean trigger = new CronTriggerFactoryBean(); trigger.setJobDetail(secondJobDetail.getObject()); // cron表达式 trigger.setCronExpression("0 30 20 * * ?"); return trigger; } // 配置Scheduler @Bean(name = "scheduler") public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger, Trigger secondTrigger) { SchedulerFactoryBean bean = new SchedulerFactoryBean(); // 延时启动,应用启动1秒后 bean.setStartupDelay(1); // 注册触发器 bean.setTriggers(firstTrigger,secondTrigger); return bean; } }