Reputation: 2113
I have successfully developed a application with SpringBoot and Quartz which executes only a single job using a Cron expression defined for that particular job. My implementation is largely based on the information found in following article.
https://chynten.wordpress.com/2016/06/17/quartz-with-databse-and-spring-4/
There, as you can see, the job triggering is done using a bean which encapsulates the cron expression.
@Bean
public Trigger simpleJobTrigger(@Qualifier("simpleJobDetail") JobDetail jobDetail) {
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(0L);
factoryBean.setName("test-trigger");
factoryBean.setStartTime(LocalDateTime.now().toDate());
factoryBean.setCronExpression("0/20 * * * * ?");
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
try {
factoryBean.afterPropertiesSet();
} catch (ParseException e) {
e.printStackTrace();
}
return factoryBean.getObject();
}
Now, I am in the need of creating multiple jobs which should be triggered based on their own cron expressions. In above case, the entire project had only one cron, hence I had no issues there. But now, different jobs should have different schedulings.
How can I extend my current implementation to support multiple jobs with different crons? Yes, I did some research on this, but couldn't find any information on how to configure Spring boot beans to cater my requirement.
Some examples have used quartz JobBuilder
to define jobs. Is that the way I should follow or can I extend my current implementation to support multiple jobs?
The question in following link looks quite similar, but I cannot understand how to pass different cron expressions and another instructions like misfire instructions to each job separately.
Autowiring in Quartz multiple Jobs with Spring Boot not working
Upvotes: 1
Views: 3937
Reputation: 400
You just need to do something like this. Just keep adding new bean for each of your new job
@Bean
public Trigger simpleJobTrigger(@Qualifier("simpleJobDetail") JobDetail jobDetail) {
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(0L);
factoryBean.setName("test-trigger");
factoryBean.setStartTime(LocalDateTime.now().toDate());
factoryBean.setCronExpression("0/20 * * * * ?");
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
try {
factoryBean.afterPropertiesSet();
} catch (ParseException e) {
e.printStackTrace();
}
return factoryBean.getObject();
}
@Bean
public Trigger sendSmsJobTrigger(@Qualifier("sendSmsJobDetail") JobDetail jobDetail) {
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(0L);
factoryBean.setName("sms-trigger");
factoryBean.setStartTime(LocalDateTime.now().toDate());
factoryBean.setCronExpression("0/20 1 1 * * ?");
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
try {
factoryBean.afterPropertiesSet();
} catch (ParseException e) {
e.printStackTrace();
}
return factoryBean.getObject();
}
Upvotes: 0
Reputation: 1386
you can add multiple triggers separated by comma as argument for setTriggers
method in schedulerFactoryBean()
from example you mentioned method since setTriggers
accept varagrs of Triggers as argument
public void setTriggers(Trigger... triggers)
refer SchedulerFactoryBean API
you can have different job details for each trigger
Upvotes: 1