В приложении 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