6

I have three environments dev, stg and prod server. I have a microservice project which is having quartz scheduler for sending daily report as email. The quartz configuration is as given below:

Now the issue is that I want the quartz scheduler for sending daily report mail to register and run only on prod environment server. I dont want the scheduler to execute under stg and dev environment server.

I am using AWS ec2 instances for environment server

Can anyone please tell me how to do this. Do we have any simple configuration with which I can achieve this

AutowiringSpringBeanJobFactory.java

public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {

    private transient AutowireCapableBeanFactory beanFactory;

    @Override
    public void setApplicationContext(final ApplicationContext context) {
        beanFactory = context.getAutowireCapableBeanFactory();
    }

    @Override
    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
        final Object job = super.createJobInstance(bundle);
        beanFactory.autowireBean(job);
        return job;
    }
}

QuartzConfiguration.java

@Configuration
@ConditionalOnProperty(name = "quartz.enabled")
public class QuartzConfiguration {

    @Autowired
    List<Trigger> listOfTrigger;

    @Bean
    public JobFactory jobFactory(ApplicationContext applicationContext) {
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource, JobFactory jobFactory) throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setOverwriteExistingJobs(true);
        factory.setDataSource(dataSource);
        factory.setJobFactory(jobFactory);
        factory.setQuartzProperties(quartzProperties());
        if (!ApplicationUtil.isObjectEmpty(listOfTrigger)) {
            factory.setTriggers(listOfTrigger.toArray(new Trigger[listOfTrigger.size()]));
        }

        return factory;
    }

    @Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    public CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression) {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(jobDetail);
        factoryBean.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
        factoryBean.setCronExpression(cronExpression);
        factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
        return factoryBean;
    }

    @SuppressWarnings("rawtypes")
    public JobDetailFactoryBean createJobDetail(Class jobClass) {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(jobClass);
        factoryBean.setDurability(true);
        return factoryBean;
    }

    @Bean
    public JobDetailFactoryBean dailyEmailJobDetail() {
        return createJobDetail(DailyReportScheduleJob.class);
    }

    @Bean(name = "dailyReportEmailSyncJobTrigger")
    public CronTriggerFactoryBean dailyReportEmailSyncJobTrigger(@Qualifier("dailyEmailJobDetail") JobDetail jobDetail, @Value("${cron.frequency.daily-report-trigger}") String frequency) {
        return createCronTrigger(jobDetail, frequency);
    }
}

quartz.properties

org.quartz.scheduler.instanceName=springBootQuartzApp
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.threadCount=5
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.tablePrefix=quartz_
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=20000
org.quartz.plugin.shutdownHook.class=org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownHook.cleanShutdown=TRUE

bootstrap.yml

quartz:
  enabled: true

cron:
  frequency:
    daily-report-trigger: 0 0 9 1/1 * ? *
spencergibb
  • 24,471
  • 6
  • 69
  • 75
Alex Man
  • 4,746
  • 17
  • 93
  • 178
  • Possible duplicate of [Enabling/disabling a web.xml filter using a Spring profile](https://stackoverflow.com/questions/30800916/enabling-disabling-a-web-xml-filter-using-a-spring-profile) – Alex R Sep 28 '19 at 19:03
  • I am using the adjustment in `application.yaml` and I am setting up an integration test to check that the scheduler is _not_ started (setting the value to `false`) and it fails..... so I guess there's something funky going on. Any ideas? – eftshift0 Jul 29 '20 at 19:32

2 Answers2

5

from your application name set in the properties, i assume you are running it as spring-boot application.

i would consider to run the the app with different profiles, each matching your environment (dev, staging, production).

refer to spring-profiles for further information

once you set the up, you can use the @Profile annotation in your quartz configuration bean to limit the loading of the bean based on the active profile(s).

Manuel Jain
  • 751
  • 1
  • 6
  • 16
5

A workaround could be to use setAutoStartup method in this way

@Bean
SchedulerFactoryBean schedulerFactoryBean() {
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
    schedulerFactoryBean.setAutoStartup(settings.enabled);
    if (settings.enabled) {
        ...
        schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);
        schedulerFactoryBean.setQuartzProperties(quartzProperties);
    } else {
        log.warn("Can't initialize Quartz because it's not enabled");
    }
    return schedulerFactoryBean;
}

So quartz won't be initialized if settings.enabled is false unless you explicitly start quartz making a call to start method.

Rodrigo Villalba Zayas
  • 5,326
  • 2
  • 23
  • 36