Commit 59a15b25 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Add Quartz Scheduler support"

Closes gh-4299
parent 9e23206c
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.quartz;
/**
* Define the supported Quartz {@code JobStore}.
*
* @author Stephane Nicoll
* @since 2.0.0
*/
public enum JobStoreType {
/**
* Store jobs in memory.
*/
MEMORY,
/**
* Store jobs in the database.
*/
JDBC
}
...@@ -28,21 +28,18 @@ import org.quartz.JobDetail; ...@@ -28,21 +28,18 @@ import org.quartz.JobDetail;
import org.quartz.Scheduler; import org.quartz.Scheduler;
import org.quartz.Trigger; import org.quartz.Trigger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
...@@ -51,6 +48,7 @@ import org.springframework.transaction.PlatformTransactionManager; ...@@ -51,6 +48,7 @@ import org.springframework.transaction.PlatformTransactionManager;
* {@link EnableAutoConfiguration Auto-configuration} for Quartz Scheduler. * {@link EnableAutoConfiguration Auto-configuration} for Quartz Scheduler.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
@Configuration @Configuration
...@@ -59,7 +57,7 @@ import org.springframework.transaction.PlatformTransactionManager; ...@@ -59,7 +57,7 @@ import org.springframework.transaction.PlatformTransactionManager;
@EnableConfigurationProperties(QuartzProperties.class) @EnableConfigurationProperties(QuartzProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, @AutoConfigureAfter({ DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class }) HibernateJpaAutoConfiguration.class })
public class QuartzAutoConfiguration implements ApplicationContextAware { public class QuartzAutoConfiguration {
private final QuartzProperties properties; private final QuartzProperties properties;
...@@ -73,23 +71,25 @@ public class QuartzAutoConfiguration implements ApplicationContextAware { ...@@ -73,23 +71,25 @@ public class QuartzAutoConfiguration implements ApplicationContextAware {
private final Trigger[] triggers; private final Trigger[] triggers;
private ApplicationContext applicationContext; private final ApplicationContext applicationContext;
public QuartzAutoConfiguration(QuartzProperties properties, public QuartzAutoConfiguration(QuartzProperties properties,
ObjectProvider<List<SchedulerFactoryBeanCustomizer>> customizers, ObjectProvider<List<SchedulerFactoryBeanCustomizer>> customizers,
ObjectProvider<Executor> taskExecutor, ObjectProvider<JobDetail[]> jobDetails, ObjectProvider<Executor> taskExecutor, ObjectProvider<JobDetail[]> jobDetails,
ObjectProvider<Map<String, Calendar>> calendars, ObjectProvider<Map<String, Calendar>> calendars,
ObjectProvider<Trigger[]> triggers) { ObjectProvider<Trigger[]> triggers,
ApplicationContext applicationContext) {
this.properties = properties; this.properties = properties;
this.customizers = customizers.getIfAvailable(); this.customizers = customizers.getIfAvailable();
this.taskExecutor = taskExecutor.getIfAvailable(); this.taskExecutor = taskExecutor.getIfAvailable();
this.jobDetails = jobDetails.getIfAvailable(); this.jobDetails = jobDetails.getIfAvailable();
this.calendars = calendars.getIfAvailable(); this.calendars = calendars.getIfAvailable();
this.triggers = triggers.getIfAvailable(); this.triggers = triggers.getIfAvailable();
this.applicationContext = applicationContext;
} }
@Bean @Bean
@ConditionalOnBean(DataSource.class) @ConditionalOnSingleCandidate(DataSource.class)
@ConditionalOnMissingBean @ConditionalOnMissingBean
public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource, public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource,
ResourceLoader resourceLoader) { ResourceLoader resourceLoader) {
...@@ -98,7 +98,7 @@ public class QuartzAutoConfiguration implements ApplicationContextAware { ...@@ -98,7 +98,7 @@ public class QuartzAutoConfiguration implements ApplicationContextAware {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public SchedulerFactoryBean schedulerFactoryBean() { public SchedulerFactoryBean quartzScheduler() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory( schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory(
this.applicationContext.getAutowireCapableBeanFactory())); this.applicationContext.getAutowireCapableBeanFactory()));
...@@ -122,12 +122,6 @@ public class QuartzAutoConfiguration implements ApplicationContextAware { ...@@ -122,12 +122,6 @@ public class QuartzAutoConfiguration implements ApplicationContextAware {
return schedulerFactoryBean; return schedulerFactoryBean;
} }
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
private Properties asProperties(Map<String, String> source) { private Properties asProperties(Map<String, String> source) {
Properties properties = new Properties(); Properties properties = new Properties();
properties.putAll(source); properties.putAll(source);
...@@ -136,7 +130,6 @@ public class QuartzAutoConfiguration implements ApplicationContextAware { ...@@ -136,7 +130,6 @@ public class QuartzAutoConfiguration implements ApplicationContextAware {
private void customize(SchedulerFactoryBean schedulerFactoryBean) { private void customize(SchedulerFactoryBean schedulerFactoryBean) {
if (this.customizers != null) { if (this.customizers != null) {
AnnotationAwareOrderComparator.sort(this.customizers);
for (SchedulerFactoryBeanCustomizer customizer : this.customizers) { for (SchedulerFactoryBeanCustomizer customizer : this.customizers) {
customizer.customize(schedulerFactoryBean); customizer.customize(schedulerFactoryBean);
} }
...@@ -144,15 +137,22 @@ public class QuartzAutoConfiguration implements ApplicationContextAware { ...@@ -144,15 +137,22 @@ public class QuartzAutoConfiguration implements ApplicationContextAware {
} }
@Configuration @Configuration
@ConditionalOnBean(DataSource.class) @ConditionalOnSingleCandidate(DataSource.class)
protected static class QuartzSchedulerDataSourceConfiguration { protected static class JdbcStoreTypeConfiguration {
@Bean @Bean
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(DataSource dataSource, public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
PlatformTransactionManager transactionManager) { QuartzProperties properties, DataSource dataSource,
ObjectProvider<PlatformTransactionManager> transactionManager) {
return schedulerFactoryBean -> { return schedulerFactoryBean -> {
schedulerFactoryBean.setDataSource(dataSource); if (properties.getJobStoreType() == JobStoreType.JDBC) {
schedulerFactoryBean.setTransactionManager(transactionManager); schedulerFactoryBean.setDataSource(dataSource);
PlatformTransactionManager txManager =
transactionManager.getIfUnique();
if (txManager != null) {
schedulerFactoryBean.setTransactionManager(txManager);
}
}
}; };
} }
......
...@@ -41,12 +41,12 @@ public class QuartzDatabaseInitializer extends AbstractDatabaseInitializer { ...@@ -41,12 +41,12 @@ public class QuartzDatabaseInitializer extends AbstractDatabaseInitializer {
@Override @Override
protected boolean isEnabled() { protected boolean isEnabled() {
return this.properties.getInitializer().isEnabled(); return this.properties.getJdbc().isInitializeSchema();
} }
@Override @Override
protected String getSchemaLocation() { protected String getSchemaLocation() {
return this.properties.getSchema(); return this.properties.getJdbc().getSchema();
} }
@Override @Override
......
...@@ -25,60 +25,69 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -25,60 +25,69 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* Configuration properties for the Quartz Scheduler integration. * Configuration properties for the Quartz Scheduler integration.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
@ConfigurationProperties("spring.quartz") @ConfigurationProperties("spring.quartz")
public class QuartzProperties { public class QuartzProperties {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/quartz/impl/"
+ "jdbcjobstore/tables_@@platform@@.sql";
private final Initializer initializer = new Initializer();
/** /**
* Additional Quartz Scheduler properties. * Quartz job store type.
*/ */
private Map<String, String> properties = new HashMap<>(); private JobStoreType jobStoreType = JobStoreType.MEMORY;
/** /**
* Path to the SQL file to use to initialize the database schema. * Additional Quartz Scheduler properties.
*/ */
private String schema = DEFAULT_SCHEMA_LOCATION; private final Map<String, String> properties = new HashMap<>();
public Initializer getInitializer() { private final Jdbc jdbc = new Jdbc();
return this.initializer;
public JobStoreType getJobStoreType() {
return this.jobStoreType;
}
public void setJobStoreType(JobStoreType jobStoreType) {
this.jobStoreType = jobStoreType;
} }
public Map<String, String> getProperties() { public Map<String, String> getProperties() {
return this.properties; return this.properties;
} }
public void setProperties(Map<String, String> properties) { public Jdbc getJdbc() {
this.properties = properties; return this.jdbc;
} }
public String getSchema() { public static class Jdbc {
return this.schema;
}
public void setSchema(String schema) { private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/quartz/impl/"
this.schema = schema; + "jdbcjobstore/tables_@@platform@@.sql";
}
public class Initializer { /**
* Path to the SQL file to use to initialize the database schema.
*/
private String schema = DEFAULT_SCHEMA_LOCATION;
/** /**
* Create the required Quartz Scheduler tables on startup if necessary. Enabled * Create the required Quartz Scheduler tables on startup.
* automatically if the schema is configured.
*/ */
private boolean enabled = true; private boolean initializeSchema;
public String getSchema() {
return this.schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public boolean isEnabled() { public boolean isInitializeSchema() {
return this.enabled && QuartzProperties.this.getSchema() != null; return this.initializeSchema;
} }
public void setEnabled(boolean enabled) { public void setInitializeSchema(boolean initializeSchema) {
this.enabled = enabled; this.initializeSchema = initializeSchema;
} }
} }
......
...@@ -28,6 +28,10 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean; ...@@ -28,6 +28,10 @@ import org.springframework.scheduling.quartz.SchedulerFactoryBean;
*/ */
public interface SchedulerFactoryBeanCustomizer { public interface SchedulerFactoryBeanCustomizer {
/**
* Customize the {@link SchedulerFactoryBean}.
* @param schedulerFactoryBean the scheduler to customize
*/
void customize(SchedulerFactoryBean schedulerFactoryBean); void customize(SchedulerFactoryBean schedulerFactoryBean);
} }
...@@ -357,6 +357,10 @@ ...@@ -357,6 +357,10 @@
"name": "spring.mvc.locale-resolver", "name": "spring.mvc.locale-resolver",
"defaultValue": "accept-header" "defaultValue": "accept-header"
}, },
{
"name": "spring.quartz.job-store-type",
"defaultValue": "memory"
},
{ {
"name": "spring.rabbitmq.cache.connection.mode", "name": "spring.rabbitmq.cache.connection.mode",
"defaultValue": "channel" "defaultValue": "channel"
......
...@@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.quartz; ...@@ -19,6 +19,8 @@ package org.springframework.boot.autoconfigure.quartz;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import javax.sql.DataSource;
import org.junit.After; import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
...@@ -30,6 +32,7 @@ import org.quartz.JobExecutionContext; ...@@ -30,6 +32,7 @@ import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import org.quartz.JobExecutionException;
import org.quartz.JobKey; import org.quartz.JobKey;
import org.quartz.Scheduler; import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger; import org.quartz.Trigger;
import org.quartz.TriggerBuilder; import org.quartz.TriggerBuilder;
...@@ -43,13 +46,16 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerA ...@@ -43,13 +46,16 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerA
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.test.rule.OutputCapture; import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.quartz.LocalDataSourceJobStore; import org.springframework.scheduling.quartz.LocalDataSourceJobStore;
import org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool; import org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool;
import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
...@@ -58,6 +64,7 @@ import static org.hamcrest.CoreMatchers.containsString; ...@@ -58,6 +64,7 @@ import static org.hamcrest.CoreMatchers.containsString;
* Tests for {@link QuartzAutoConfiguration}. * Tests for {@link QuartzAutoConfiguration}.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
*/ */
public class QuartzAutoConfigurationTests { public class QuartzAutoConfigurationTests {
...@@ -67,7 +74,7 @@ public class QuartzAutoConfigurationTests { ...@@ -67,7 +74,7 @@ public class QuartzAutoConfigurationTests {
@Rule @Rule
public OutputCapture output = new OutputCapture(); public OutputCapture output = new OutputCapture();
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private ConfigurableApplicationContext context;
@After @After
public void closeContext() { public void closeContext() {
...@@ -77,89 +84,123 @@ public class QuartzAutoConfigurationTests { ...@@ -77,89 +84,123 @@ public class QuartzAutoConfigurationTests {
} }
@Test @Test
public void withDatabase() throws Exception { public void withNoDataSource() throws Exception {
registerAndRefresh(EmbeddedDataSourceConfiguration.class, load();
DataSourceTransactionManagerAutoConfiguration.class, assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
QuartzAutoConfiguration.class);
Scheduler scheduler = this.context.getBean(Scheduler.class); Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler).isNotNull();
assertThat(scheduler.getMetaData().getJobStoreClass()) assertThat(scheduler.getMetaData().getJobStoreClass())
.isAssignableFrom(LocalDataSourceJobStore.class); .isAssignableFrom(RAMJobStore.class);
} }
@Test @Test
public void withNoDatabase() throws Exception { public void withDataSourceUseMemoryByDefault() throws Exception {
registerAndRefresh(QuartzAutoConfiguration.class); load(new Class<?>[] { EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class });
assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
Scheduler scheduler = this.context.getBean(Scheduler.class); Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler).isNotNull();
assertThat(scheduler.getMetaData().getJobStoreClass()) assertThat(scheduler.getMetaData().getJobStoreClass())
.isAssignableFrom(RAMJobStore.class); .isAssignableFrom(RAMJobStore.class);
} }
@Test @Test
public void withTaskExecutor() throws Exception { public void withDataSource() throws Exception {
registerAndRefresh(QuartzAutoConfiguration.class, load(new Class<?>[] { QuartzJobsConfiguration.class,
QuartzExecutorConfiguration.class); EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class },
"spring.quartz.job-store-type=jdbc",
"spring.quartz.jdbc.initialize-schema=true");
testWithDataSource();
}
@Test
public void withDataSourceNoTransactionManager() throws Exception {
load(new Class<?>[] { QuartzJobsConfiguration.class,
EmbeddedDataSourceConfiguration.class },
"spring.quartz.job-store-type=jdbc",
"spring.quartz.jdbc.initialize-schema=true");
testWithDataSource();
}
private void testWithDataSource() throws SchedulerException {
assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
Scheduler scheduler = this.context.getBean(Scheduler.class); Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler.getMetaData().getJobStoreClass())
.isAssignableFrom(LocalDataSourceJobStore.class);
JdbcTemplate jdbcTemplate = new JdbcTemplate(
this.context.getBean(DataSource.class));
assertThat(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM QRTZ_JOB_DETAILS",
Integer.class)).isEqualTo(2);
assertThat(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM QRTZ_SIMPLE_TRIGGERS",
Integer.class)).isEqualTo(0);
}
assertThat(scheduler).isNotNull(); @Test
public void withTaskExecutor() throws Exception {
load(QuartzExecutorConfiguration.class);
assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler.getMetaData().getThreadPoolClass()) assertThat(scheduler.getMetaData().getThreadPoolClass())
.isEqualTo(LocalTaskExecutorThreadPool.class); .isEqualTo(LocalTaskExecutorThreadPool.class);
} }
@Test @Test
public void withConfiguredJobAndTrigger() throws Exception { public void withConfiguredJobAndTrigger() throws Exception {
TestPropertyValues.of("test-name=withConfiguredJobAndTrigger") load(QuartzFullConfiguration.class, "test-name=withConfiguredJobAndTrigger");
.applyTo(this.context); assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
registerAndRefresh(QuartzAutoConfiguration.class, QuartzJobConfiguration.class);
Scheduler scheduler = this.context.getBean(Scheduler.class); Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler.getJobDetail(JobKey.jobKey("fooJob"))).isNotNull(); assertThat(scheduler.getJobDetail(JobKey.jobKey("fooJob"))).isNotNull();
assertThat(scheduler.getTrigger(TriggerKey.triggerKey("fooTrigger"))).isNotNull(); assertThat(scheduler.getTrigger(TriggerKey.triggerKey("fooTrigger"))).isNotNull();
Thread.sleep(1000L); Thread.sleep(1000L);
this.output.expect(containsString("withConfiguredJobAndTrigger")); this.output.expect(containsString("withConfiguredJobAndTrigger"));
this.output.expect(containsString("jobDataValue"));
} }
@Test @Test
public void withConfiguredCalendars() throws Exception { public void withConfiguredCalendars() throws Exception {
registerAndRefresh(QuartzAutoConfiguration.class, load(QuartzCalendarsConfiguration.class);
QuartzCalendarsConfiguration.class); assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
Scheduler scheduler = this.context.getBean(Scheduler.class); Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler.getCalendar("weekly")).isNotNull(); assertThat(scheduler.getCalendar("weekly")).isNotNull();
assertThat(scheduler.getCalendar("monthly")).isNotNull(); assertThat(scheduler.getCalendar("monthly")).isNotNull();
} }
@Test @Test
public void withQuartzProperties() throws Exception { public void withQuartzProperties() throws Exception {
TestPropertyValues load("spring.quartz.properties.org.quartz.scheduler.instanceId=FOO");
.of("spring.quartz.properties.org.quartz.scheduler.instanceId=FOO") assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
.applyTo(this.context);
registerAndRefresh(QuartzAutoConfiguration.class);
Scheduler scheduler = this.context.getBean(Scheduler.class); Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler).isNotNull();
assertThat(scheduler.getSchedulerInstanceId()).isEqualTo("FOO"); assertThat(scheduler.getSchedulerInstanceId()).isEqualTo("FOO");
} }
@Test @Test
public void withCustomizer() throws Exception { public void withCustomizer() throws Exception {
registerAndRefresh(QuartzAutoConfiguration.class, QuartzCustomConfig.class); load(QuartzCustomConfig.class);
assertThat(this.context.getBeansOfType(Scheduler.class)).hasSize(1);
Scheduler scheduler = this.context.getBean(Scheduler.class); Scheduler scheduler = this.context.getBean(Scheduler.class);
assertThat(scheduler).isNotNull();
assertThat(scheduler.getSchedulerName()).isEqualTo("fooScheduler"); assertThat(scheduler.getSchedulerName()).isEqualTo("fooScheduler");
} }
private void registerAndRefresh(Class<?>... annotatedClasses) { private void load(String... environment) {
this.context.register(annotatedClasses); load(new Class<?>[0], environment);
this.context.refresh(); }
private void load(Class<?> config, String... environment) {
load(new Class<?>[] { config }, environment);
}
private void load(Class<?>[] configs, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
TestPropertyValues.of(environment).applyTo(ctx);
if (!ObjectUtils.isEmpty(configs)) {
ctx.register(configs);
}
ctx.register(QuartzAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
} }
@Configuration @Configuration
protected static class QuartzJobConfiguration { protected static class QuartzJobsConfiguration {
@Bean @Bean
public JobDetail fooJob() { public JobDetail fooJob() {
...@@ -167,6 +208,24 @@ public class QuartzAutoConfigurationTests { ...@@ -167,6 +208,24 @@ public class QuartzAutoConfigurationTests {
.storeDurably().build(); .storeDurably().build();
} }
@Bean
public JobDetail barJob() {
return JobBuilder.newJob().ofType(FooJob.class).withIdentity("barJob")
.storeDurably().build();
}
}
@Configuration
protected static class QuartzFullConfiguration {
@Bean
public JobDetail fooJob() {
return JobBuilder.newJob().ofType(FooJob.class).withIdentity("fooJob")
.usingJobData("jobDataKey", "jobDataValue")
.storeDurably().build();
}
@Bean @Bean
public Trigger fooTrigger() { public Trigger fooTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule() SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
...@@ -219,10 +278,17 @@ public class QuartzAutoConfigurationTests { ...@@ -219,10 +278,17 @@ public class QuartzAutoConfigurationTests {
@Autowired @Autowired
private Environment env; private Environment env;
private String jobDataKey;
@Override @Override
protected void executeInternal(JobExecutionContext context) protected void executeInternal(JobExecutionContext context)
throws JobExecutionException { throws JobExecutionException {
System.out.println(this.env.getProperty("test-name", "unknown")); System.out.println(this.env.getProperty("test-name", "unknown") + " - "
+ this.jobDataKey);
}
public void setJobDataKey(String jobDataKey) {
this.jobDataKey = jobDataKey;
} }
} }
......
...@@ -130,9 +130,10 @@ content into your application; rather pick only the properties that you need. ...@@ -130,9 +130,10 @@ content into your application; rather pick only the properties that you need.
spring.profiles.include= # Unconditionally activate the specified comma separated profiles (or list of profiles if using YAML). spring.profiles.include= # Unconditionally activate the specified comma separated profiles (or list of profiles if using YAML).
# QUARTZ SCHEDULER ({sc-spring-boot-autoconfigure}/quartz/QuartzProperties.{sc-ext}[QuartzProperties]) # QUARTZ SCHEDULER ({sc-spring-boot-autoconfigure}/quartz/QuartzProperties.{sc-ext}[QuartzProperties])
spring.quartz.initializer.enabled=true # Create the required Quartz Scheduler tables on startup if necessary. Enabled automatically if the schema is configured. spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties. spring.quartz.properties.*= # Additional Quartz Scheduler properties.
spring.quartz.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema. spring.quartz.jdbc.initialize-schema=false # Create the required Quartz Scheduler tables on startup.
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
# Reactor # Reactor
spring.reactor.stacktrace-mode.enabled=false # Set whether Reactor should collect stacktrace information at runtime. spring.reactor.stacktrace-mode.enabled=false # Set whether Reactor should collect stacktrace information at runtime.
......
...@@ -5153,32 +5153,66 @@ caching is enabled. ...@@ -5153,32 +5153,66 @@ caching is enabled.
[[boot-features-quartz]] [[boot-features-quartz]]
== Quartz Scheduler == Quartz Scheduler
Spring Boot offers several conveniences for working with the Quartz scheduler, including
the `spring-boot-starter-quartz` '`Starter`'. If Quartz is available, a `Scheduler` will
be auto-configured (via the `SchedulerFactoryBean` abstraction).
If Quartz Scheduler and the relevant libraries (as defined by `spring-boot-starter-quartz`) Beans of the following types will be automatically picked up and associated with the
are on the classpath, Spring Boot will auto-configure a `SchedulerFactoryBean` which the `Scheduler`:
provides `Scheduler` instance that you can inject in your application.
Beans of following types will be automatically picked up and added to * `JobDetail`: defines a particular Job. `JobDetail` instance can easily be built with
`SchedulerFactoryBean`: the `JobBuilder` API
* `JobDetail`
* `Calendar` * `Calendar`
* `Trigger` * `Trigger`: defines when a particular job is triggered
By default, an in-memory `JobStore` will be used. However, it is possible to configure
a JDBC-based store if a `DataSource` bean is available in your application and if the
`spring.quartz.job-store-type` property is configured accordingly:
[source,properties,indent=0]
----
spring.quartz.job-store-type=jdbc
----
When the jdbc store is used, the schema can be initialized on startup:
[source,properties,indent=0]
----
spring.quartz.jdbc.initialize-schema=true
----
NOTE: The database is detected by default and initialized using the standard scripts
provided with the Quartz library. It is also possible to provide a custom script using the
`spring.quartz.jdbc.schema` property.
By default, an in-memory `JobStore` will be used. However, if `DataSource` bean is Quartz Scheduler configuration can be customized using Quartz configuration properties (see
available in your application, Quartz Scheduler will be configured with a persistent `spring.quartz.properties.*`) and `SchedulerFactoryBeanCustomizer` beans which allows
`JobStore`. programmatic `SchedulerFactoryBean` customization.
When using a persistent `JobStore`, Quartz database schema can be initialized using Job can define setters to inject data map properties. Regular beans can also be injected
`QuartzDatabaseInitializer` if the location of schema script is configured using in a similar manner:
`spring.quartz.schema` property.
Quartz Scheduler configuration can also be customized using Quartz configuration properties [source,java,indent=0]
(see `spring.quartz.properties.*`) and `SchedulerFactoryBeanCustomizer` beans which ----
allows programmatic `SchedulerFactoryBean` customization. public class SampleJob extends QuartzJobBean {
private MyService myService;
private String name;
// Inject "MyService" bean
public void setMyService(MyService myService) { ... }
// Inject the "name" job data property
public void setName(String name) { ... }
Spring Boot also configures `JobFactory` that is `@Autowire` capable so you can easily @Override
inject beans from `applicationContext` and use them in your Quartz jobs. protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
...
}
}
----
......
...@@ -8,4 +8,4 @@ The sample uses Maven. It can be built and run from the command line: ...@@ -8,4 +8,4 @@ The sample uses Maven. It can be built and run from the command line:
$ mvn spring-boot:run $ mvn spring-boot:run
---- ----
Console log will now show Hello message from SampleJob every 10 seconds. Console log will now show "Hello World!" from `SampleJob` every 2 seconds.
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.quartz; package sample.quartz;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.scheduling.quartz.QuartzJobBean;
public class SampleJob extends QuartzJobBean { public class SampleJob extends QuartzJobBean {
private String name;
private static final Logger LOGGER = LoggerFactory.getLogger(SampleJob.class); // Invoked if a Job data map entry with that name
public void setName(String name) {
this.name = name;
}
@Override @Override
protected void executeInternal(JobExecutionContext context) protected void executeInternal(JobExecutionContext context)
throws JobExecutionException { throws JobExecutionException {
LOGGER.info("Hello {}!", context.getJobDetail().getKey()); System.out.println(String.format("Hello %s!", this.name));
} }
} }
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.quartz; package sample.quartz;
import org.quartz.JobBuilder; import org.quartz.JobBuilder;
...@@ -18,17 +34,18 @@ public class SampleQuartzApplication { ...@@ -18,17 +34,18 @@ public class SampleQuartzApplication {
} }
@Bean @Bean
public JobDetail jobDetail() { public JobDetail sampleJobDetail() {
return JobBuilder.newJob().ofType(SampleJob.class).withIdentity("sampleJob") return JobBuilder.newJob().ofType(SampleJob.class).withIdentity("sampleJob")
.usingJobData("name", "World")
.storeDurably().build(); .storeDurably().build();
} }
@Bean @Bean
public Trigger trigger() { public Trigger sampleJobTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule() SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10).repeatForever(); .withIntervalInSeconds(2).repeatForever();
return TriggerBuilder.newTrigger().forJob(jobDetail()) return TriggerBuilder.newTrigger().forJob(sampleJobDetail())
.withIdentity("sampleTrigger").withSchedule(scheduleBuilder).build(); .withIdentity("sampleTrigger").withSchedule(scheduleBuilder).build();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment