如何为 dev 和 stg 环境禁用 Quartz 调度程序 [英] How to disable Quartz scheduler for dev and stg environment
问题描述
我有三个环境 dev、stg 和 prod 服务器.我有一个微服务项目,它有石英调度程序,用于将每日报告作为电子邮件发送.石英配置如下:
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:
现在的问题是,我希望用于发送每日报告邮件的石英调度程序仅在生产环境服务器上注册和运行.我不希望调度程序在 stg 和 dev 环境服务器下执行.
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.
我使用 AWS ec2 实例作为环境服务器
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 * ? *
推荐答案
根据您在属性中设置的应用程序名称,我假设您将其作为 spring-boot 应用程序运行.
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).
参考spring-profiles 了解更多信息
设置完成后,您可以在石英配置 bean 中使用 @Profile
注释来限制基于活动配置文件的 bean 的加载.
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).
这篇关于如何为 dev 和 stg 环境禁用 Quartz 调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!