参考: https://www.hangge.com/blog/cache/detail_2699.html
(1)pom添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
(2)
package com.xxx;
import org.quartz.CronTrigger; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration public class QuartzConfig {
@Value("${demo.scheduler.time}") private String demoTime; @Bean MethodInvokingJobDetailFactoryBean jobDetail1() { MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean(); bean.setTargetBeanName("demoJobService"); bean.setTargetMethod("execute"); return bean; }
@Bean CronTriggerFactoryBean cronTrigger() { CronTriggerFactoryBean bean = new CronTriggerFactoryBean(); bean.setJobDetail(jobDetail1().getObject()); // 设置 jobDetail bean.setCronExpression(demoTime); // 设置 Cron 表达式 return bean; }
/********** 创建 SchedulerFactory **********/ @Bean SchedulerFactoryBean schedulerFactory() { SchedulerFactoryBean bean = new SchedulerFactoryBean();
CronTrigger cronTrigger = cronTrigger().getObject(); bean.setTriggers(cronTrigger); return bean; } }
(3)行了。
|