Spring boot integrates quartz to implement multiple scheduled tasks. springquartz
I recently received many emails asking how spring boot integrates quartz to implement multiple scheduled tasks. Since I have not used multiple scheduled tasks in production, here is an implementation idea.
1. Create two scheduled 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 preceding two tasks
@ Component public class SchedulerAllJob {@ Autowired private SchedulerFactoryBean schedulerFactoryBean;/** database operations can be injected here, query all task configurations * // *** this method is used to start all scheduled tasks * @ throws SchedulerException */public void scheduleJobs () throws SchedulerException {Scheduler schedjobs = schedulerFactoryBean. getScheduler ();/*****/scheduleJob1 (schedb1); scheduleJob2 (schedb2);}/***** configure Job1 * the job can be placed in properties. Or put it in the database * If you need dynamic timing tasks at this time, please refer to: http://blog.csdn.net/liuchuanhong1/article/details/60873295 * blog ScheduleRefreshDatabase class * @ param scheduler * @ throws SchedulerException */private void scheduleJob1 (Scheduler scheduler) throws SchedulerException {/** you can query the database by task name. If the database contains this task, follow the method in the ScheduleRefreshDatabase class, update the task configuration and trigger * if the task is not found in the database at this time, create a task and configure the initialization parameters according to the following steps, and save the configuration to the database */JobDetail jobDet Ail = JobBuilder. newJob (ScheduledJob. class ). withIdentity ("job1", "group1 "). build (); // execute CronScheduleBuilder scheduleBuilder = CronScheduleBuilder every 5s. cronSchedule ("0/5 ****? "); CronTrigger cronTrigger = TriggerBuilder. newTrigger (). withIdentity ("trigger1", "group1 "). withSchedule (scheduleBuilder ). build (); scheduler. scheduleJob (rule, cronTrigger);}/*** configure Job * @ param scheduler * @ throws SchedulerException */private void scheduleJob2 (Scheduler scheduler) throws SchedulerException {response = JobBuilder. newJob (ScheduledJob2.class ). withIdentity ("job2 ", "Group1"). build (); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder. cronSchedule ("0/10 ****? "); // Execute CronTrigger cronTrigger = TriggerBuilder every 10 seconds. 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;/*** run this method at startup, or use ApplicationListener, run this method at startup * See http://blog.csdn.net/liuchuanhong1/article/details/77568187 * @ throws SchedulerException */@ Scheduled (cron = "0 08 18? ** ") Public void schedule () throws SchedulerException {myschedjobs. scheduleJobs () ;}@ Bean public writable listener () {define SchedulerFactoryBean = new schedulerFactoryBean (); return response ;}}
4. The test results are as follows:
schedule job1 is running…………………………………… schedule job2 is running …………………………………………………… schedule job1 is running…………………………………… schedule job1 is running…………………………………… schedule job2 is running …………………………………………………… schedule job1 is running…………………………………… schedule job1 is running…………………………………… schedule job2 is running …………………………………………………… schedule job1 is running……………………………………
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.