在Spring Boot应用程序中,我有一个Quartz作业,其中包含针对集群环境的设置,以避免同时运行此作业。
为了进行测试,该应用程序的两个实例在同一台计算机上运行。
这不是我所期望的,两个实例都运行计划的作业。您能为我建议如何在单个应用程序实例上运行作业吗?
application.properties中的设置:
quartz.awb.notification.expression = 0 30 19吗? * * *
QuartzConfig类:
@Configuration
@Slf4j
public class QuartzConfig {
@Value("${quartz.awb.notification.expression}")
private String expressionNotification;
@Value("${quartz.configFile}")
private String configFile;
@Bean
public SpringBeanJobFactory springBeanJobFactory() {
return new AutowiringSpringBeanJobFactory();
}
@Bean
public SchedulerFactory getSchedulerFactory() throws SchedulerException {
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
stdSchedulerFactory.initialize(configFile);
Scheduler scheduler = stdSchedulerFactory.getScheduler();
scheduler.start();
if (scheduler.checkExists(notificationJobDetail().getKey())){
scheduler.deleteJob(notificationJobDetail().getKey());
}
scheduler.scheduleJob(notificationJobDetail(),notificationJobTrigger());
return stdSchedulerFactory;
}
@Bean
public Trigger notificationJobTrigger() {
return TriggerBuilder.newTrigger()
.forJob(this.notificationJobDetail())
.withIdentity("reminderTrigger","sms")
.withSchedule(
CronScheduleBuilder.cronSchedule(expressionNotification)
)
.build();
}
@Bean
public JobDetail notificationJobDetail() {
JobDetail jobdetails = JobBuilder
.newJob(ReminderJob.class)
.withIdentity("reminder", "sms")
.storeDurably()
.build();
return jobdetails;
}}
@Slf4j
@DisallowConcurrentExecution
public class ReminderJob extends QuartzJobBean {
@Autowired
private NotificationService notificationService;
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
log.info("Quartz, execute send notification reminder");
this.notificationService.sendNotificationReminder();
}
}
qurtz.properties:
# Configure Main Scheduler Properties
org.quartz.scheduler.instanceName=my.scheduler
org.quartz.scheduler.instanceId=AUTO
# Configure ThreadPool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=5
org.quartz.threadPool.threadPriority=5
# Configure Quartz DataSource
org.quartz.dataSource.my.driver=oracle.jdbc.driver.OracleDriver
org.quartz.dataSource.my.URL=jdbc:oracle:thin:@ldap://...
org.quartz.dataSource.my.user=username
org.quartz.dataSource.my.password=...
org.quartz.dataSource.my.maxConnections=5
org.quartz.dataSource.my.validationQuery=select 0 from dual
# Configure JobStore
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource=my
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.acquireTriggersWithinLock = true