spring boot integrates quartz to achieve multiple timed tasks
- 2020-12-22 17:37:49
- OfStack
Recently, I have received a lot of emails, all wanting to know how spring boot integrates quartz to achieve multiple timing tasks. Since I did not use multiple timing tasks in production, here is the idea of implementation.
1. Create two new timed 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 timing tasks
* @throws SchedulerException
*/
public void scheduleJobs() throws SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
/**
*
*/
scheduleJob1(scheduler);
scheduleJob2(scheduler);
}
/**
* configuration Job1
* The tasks here can be configured and placed properties Or you could put it in a database
* If you need to do dynamic timed tasks at this time, please refer to: http://blog.csdn.net/liuchuanhong1/article/details/60873295
* In the blog ScheduleRefreshDatabase class
* @param scheduler
* @throws SchedulerException
*/
private void scheduleJob1(Scheduler scheduler) throws SchedulerException{
/*
* Here you can first query the database by task name, if the task exists in the database, according to ScheduleRefreshDatabase Class, configuration of update tasks, and triggers
* If the task is not found in the database at this point, follow the steps below to create it 1 Task, and configure the initialization parameters, and save the configuration to the database
*/
JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
// every 5s perform 1 time
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
/**
* configuration 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 * * * * ?");
// every 10s perform 1 time
CronTrigger 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 the method on startup, or use ApplicationListener , executes the method on startup
* For specific use, please refer to: 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 job2 is running .........................................................................................................
schedule job1 is running .....................................................................
schedule job1 is running .....................................................................
schedule job2 is running .........................................................................................................
schedule job1 is running .....................................................................