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