springBoot整合quartz
文章目录
- springBoot整合quartz
导坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
定义任务,不需要定义为Bean,要继承QuartzJobBean
public class MyQuartz extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("quartz task run...");
}
}
绑关系
@Configuration
public class QuartzConfig {
// 创建工作明细
@Bean
public JobDetail printJobDetail(){
// 绑定具体的工作
return JobBuilder
.newJob(MyQuartz.class)
.storeDurably()
.build();
}
// 创建触发器
@Bean
public Trigger printJobTrigger(){
ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/15 * * * * ?");
// 绑定对应的工作明细
return TriggerBuilder
.newTrigger()
.forJob(printJobDetail())
.withSchedule(scheduleBuilder)
.build();
}
}