Merge pull request #13582 from tdanylchuk:configure-overwriteExistingJobs-via-properties
* pr/13582: Polish "Allow to configure Quartz's "overwriteExistingJobs" property" Allow to configure Quartz's "overwriteExistingJobs" property
This commit is contained in:
@@ -90,6 +90,8 @@ public class QuartzAutoConfiguration {
|
||||
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
|
||||
schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory(
|
||||
this.applicationContext.getAutowireCapableBeanFactory()));
|
||||
schedulerFactoryBean
|
||||
.setOverwriteExistingJobs(this.properties.isOverwriteExistingJobs());
|
||||
if (!this.properties.getProperties().isEmpty()) {
|
||||
schedulerFactoryBean
|
||||
.setQuartzProperties(asProperties(this.properties.getProperties()));
|
||||
|
||||
@@ -37,6 +37,11 @@ public class QuartzProperties {
|
||||
*/
|
||||
private JobStoreType jobStoreType = JobStoreType.MEMORY;
|
||||
|
||||
/**
|
||||
* Whether configured jobs should overwrite existing job definitions.
|
||||
*/
|
||||
private boolean overwriteExistingJobs = false;
|
||||
|
||||
/**
|
||||
* Additional Quartz Scheduler properties.
|
||||
*/
|
||||
@@ -52,6 +57,14 @@ public class QuartzProperties {
|
||||
this.jobStoreType = jobStoreType;
|
||||
}
|
||||
|
||||
public boolean isOverwriteExistingJobs() {
|
||||
return this.overwriteExistingJobs;
|
||||
}
|
||||
|
||||
public void setOverwriteExistingJobs(boolean overwriteExistingJobs) {
|
||||
this.overwriteExistingJobs = overwriteExistingJobs;
|
||||
}
|
||||
|
||||
public Map<String, String> getProperties() {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobKey;
|
||||
import org.quartz.Scheduler;
|
||||
import org.quartz.SimpleScheduleBuilder;
|
||||
import org.quartz.SimpleTrigger;
|
||||
import org.quartz.Trigger;
|
||||
import org.quartz.TriggerBuilder;
|
||||
import org.quartz.TriggerKey;
|
||||
@@ -158,6 +159,21 @@ public class QuartzAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withOverwriteExistingJobs() {
|
||||
this.contextRunner.withUserConfiguration(OverwriteTriggerConfiguration.class)
|
||||
.withPropertyValues("spring.quartz.overwrite-existing-jobs=true")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(Scheduler.class);
|
||||
Scheduler scheduler = context.getBean(Scheduler.class);
|
||||
Trigger fooTrigger = scheduler
|
||||
.getTrigger(TriggerKey.triggerKey("fooTrigger"));
|
||||
assertThat(fooTrigger).isNotNull();
|
||||
assertThat(((SimpleTrigger) fooTrigger).getRepeatInterval())
|
||||
.isEqualTo(30000);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withConfiguredJobAndTrigger() {
|
||||
this.contextRunner.withUserConfiguration(QuartzFullConfiguration.class)
|
||||
@@ -251,6 +267,21 @@ public class QuartzAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(QuartzFullConfiguration.class)
|
||||
protected static class OverwriteTriggerConfiguration extends BaseQuartzConfiguration {
|
||||
|
||||
@Bean
|
||||
public Trigger anotherFooTrigger(JobDetail fooJob) {
|
||||
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
|
||||
.withIntervalInSeconds(30).repeatForever();
|
||||
|
||||
return TriggerBuilder.newTrigger().forJob(fooJob).withIdentity("fooTrigger")
|
||||
.withSchedule(scheduleBuilder).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class QuartzCalendarsConfiguration extends BaseQuartzConfiguration {
|
||||
|
||||
|
||||
@@ -146,6 +146,7 @@ content into your application. Rather, pick only the properties that you need.
|
||||
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
|
||||
spring.quartz.job-store-type=memory # Quartz job store type.
|
||||
spring.quartz.properties.*= # Additional Quartz Scheduler properties.
|
||||
spring.quartz.overwrite-existing-jobs=false # Whether configured jobs should overwrite existing job definitions.
|
||||
|
||||
# REACTOR ({sc-spring-boot-autoconfigure}/reactor/core/ReactorCoreProperties.{sc-ext}[ReactorCoreProperties])
|
||||
spring.reactor.stacktrace-mode.enabled=false # Whether Reactor should collect stacktrace information at runtime.
|
||||
|
||||
@@ -5996,8 +5996,12 @@ NOTE: By default, the database is detected and initialized by using the standard
|
||||
provided with the Quartz library. It is also possible to provide a custom script by
|
||||
setting the `spring.quartz.jdbc.schema` property.
|
||||
|
||||
By default, jobs created by configuration will not overwrite already registered jobs that
|
||||
have been read from a persistent job store. To enable overwriting existing job definitions
|
||||
set the `spring.quartz.overwrite-existing-jobs` property.
|
||||
|
||||
Quartz Scheduler configuration can be customized by using Quartz configuration properties
|
||||
()`spring.quartz.properties.*`) and `SchedulerFactoryBeanCustomizer` beans, which allow
|
||||
(`spring.quartz.properties.*`) and `SchedulerFactoryBeanCustomizer` beans, which allow
|
||||
programmatic `SchedulerFactoryBean` customization.
|
||||
|
||||
NOTE: In particular, an `Executor` bean is not associated with the scheduler as Quartz
|
||||
|
||||
Reference in New Issue
Block a user