Reputation: 23277
We have an service with a quartz scheduler.
This service can be scaled accordingly some needs.
Our Quartz scheduler is not in a cluster mode.
So, we need to able or disable scheduler according to an environment variable.
Service can't be splitted in order to have two independent services.
This is our related Quartz configuration class:
@Configuration
public class QuartzSchedulerConfiguration {
private static final String HOURLY_CRON_EXPRESSION = "0 0 * * * ?";
private static final String MIDNIGHT_CRON_EXPRESSION = "0 0 0 * * ?";
@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, Trigger[] fitxersJobTrigger)
throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setOverwriteExistingJobs(true);
factory.setAutoStartup(true);
factory.setJobFactory(jobFactory);
factory.setQuartzProperties(quartzProperties());
factory.setTriggers(fitxersJobTrigger);
return factory;
}
@Bean
public JobDetailFactoryBean loadPendingDocumentsJobDetail() {
return createJobDetailFactoryBean(LoadPendingDocumentsJob.class);
}
@Bean
public SimpleTriggerFactoryBean loadPendingDocumentsJobTrigger(
@Qualifier("loadPendingDocumentsJobDetail") JobDetail jobDetail) {
long interval = jobsConfiguration.get().getParameters().stream()
.filter(param -> "loadPendingDocumentsJobInterval".equals(param.getName()))
.findAny()
.map(param -> (Integer)param.getValue())
.orElse(600000); // every 10 minutes
LOG.debug("loadPendingDocumentsJobInterval = " + interval);
return createIntervalTriggerFactoryBean(jobDetail, interval);
}
private CronTriggerFactoryBean createCronTriggerFactoryBean(JobDetail jobDetail, String expression) {
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setCronExpression(expression);
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
return factoryBean;
}
private JobDetailFactoryBean createJobDetailFactoryBean(Class<? extends Job> jobClass) {
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
factoryBean.setJobClass(jobClass);
factoryBean.setDurability(true);
return factoryBean;
}
private SimpleTriggerFactoryBean createIntervalTriggerFactoryBean(JobDetail jobDetail, long interval) {
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(0L);
factoryBean.setRepeatInterval(interval);
factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
return factoryBean;
}
}
Any ideas?
Upvotes: 1
Views: 2130
Reputation: 856
You can try using the @ConditionalOnProperty
Annotation on your configuration class.
Upvotes: 1