I have received many emails recently, all of whom want to know how spring boot integrates quartz to implement multiple timing tasks. Since I do not use multiple timing tasks in production, I will give me an idea for implementation.
1. Create two new timing tasks, as follows:
public class ScheduledJob implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("schedule job1 is running…………………………………………………………… “); } } public class ScheduledJob2 implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("schedule job2 is running …………………………………………………”); } }2. Configure the above two tasks
@Component public class SchedulerAllJob { @Autowired private SchedulerFactoryBean schedulerFactoryBean; /* * Here you can inject database operations to query all task configurations*/ /** * This method is used to start all timed tasks* @throws SchedulerException */ public void scheduleJobs() throws SchedulerException { Scheduler scheduler = schedulerFactoryBean.getScheduler(); /** * */ scheduleJob1(scheduler); scheduleJob2(scheduler); } /** * Configure Job1 * The tasks here can be configured and placed in properties or in the database* If you need to do dynamic timing tasks at this time, please refer to: http://blog.csdn.net/liuchuanhong1/article/details/60873295 * ScheduleRefreshDatabase class in the blog* @param scheduler * @throws SchedulerException */ private void scheduleJob1(Scheduler scheduler) throws SchedulerException{ /* * Here you can query the database first by the task name. If the task exists in the database, update the task configuration and triggers according to the methods in the ScheduleRefreshDatabase class* If the task is not queryed in the database at this time, follow the steps below to create a new task, configure the initialization parameters, and store the configuration in the database */ JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build(); // Execute CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * ?"); CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail,cronTrigger); } /** * Configure Job * @param scheduler * @throws SchedulerException */ private void scheduleJob2(Scheduler scheduler) throws SchedulerException{ JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job2", "group1").build(); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?"); // CronTrigger executes every 10 seconds cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail,cronTrigger); } }3. Start two tasks
@Configuration @EnableScheduling @Component public class SchedulerListener { @Autowired public SchedulerAllJob myScheduler; /** * Execute this method at startup, or use ApplicationListener to execute the method at startup* For specific use, see: http://blog.csdn.net/liuchuanhong1/article/details/77568187 * @throws SchedulerException */ @Scheduled(cron="0 08 18 ? * *") public void schedule() throws SchedulerException { myScheduler.scheduleJobs(); } @Bean public SchedulerFactoryBean schedulerFactoryBean(){ SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); return schedulerFactoryBean; } }4. The test results are as follows
schedule job1 is running………………………………… schedule job2 is running…………………………………… schedule job1 is running………………………………… schedule job1 is running……………………………………… schedule job1 is running………………………………………… schedule job1 is running………………………………………… schedule job1 is running………………………………………………… schedule job1 is running………………………………………………………………… schedule job1 is running……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.