Skip to content

Commit 59a15b2

Browse files
committed
Polish "Add Quartz Scheduler support"
Closes gh-4299
1 parent 9e23206 commit 59a15b2

File tree

12 files changed

+309
-118
lines changed

12 files changed

+309
-118
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.quartz;
18+
19+
/**
20+
* Define the supported Quartz {@code JobStore}.
21+
*
22+
* @author Stephane Nicoll
23+
* @since 2.0.0
24+
*/
25+
public enum JobStoreType {
26+
27+
/**
28+
* Store jobs in memory.
29+
*/
30+
MEMORY,
31+
32+
/**
33+
* Store jobs in the database.
34+
*/
35+
JDBC
36+
37+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,18 @@
2828
import org.quartz.Scheduler;
2929
import org.quartz.Trigger;
3030

31-
import org.springframework.beans.BeansException;
3231
import org.springframework.beans.factory.ObjectProvider;
3332
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
3433
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
35-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3634
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3735
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
36+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
3837
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3938
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
4039
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4140
import org.springframework.context.ApplicationContext;
42-
import org.springframework.context.ApplicationContextAware;
4341
import org.springframework.context.annotation.Bean;
4442
import org.springframework.context.annotation.Configuration;
45-
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
4643
import org.springframework.core.io.ResourceLoader;
4744
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
4845
import org.springframework.transaction.PlatformTransactionManager;
@@ -51,6 +48,7 @@
5148
* {@link EnableAutoConfiguration Auto-configuration} for Quartz Scheduler.
5249
*
5350
* @author Vedran Pavic
51+
* @author Stephane Nicoll
5452
* @since 2.0.0
5553
*/
5654
@Configuration
@@ -59,7 +57,7 @@
5957
@EnableConfigurationProperties(QuartzProperties.class)
6058
@AutoConfigureAfter({ DataSourceAutoConfiguration.class,
6159
HibernateJpaAutoConfiguration.class })
62-
public class QuartzAutoConfiguration implements ApplicationContextAware {
60+
public class QuartzAutoConfiguration {
6361

6462
private final QuartzProperties properties;
6563

@@ -73,23 +71,25 @@ public class QuartzAutoConfiguration implements ApplicationContextAware {
7371

7472
private final Trigger[] triggers;
7573

76-
private ApplicationContext applicationContext;
74+
private final ApplicationContext applicationContext;
7775

7876
public QuartzAutoConfiguration(QuartzProperties properties,
7977
ObjectProvider<List<SchedulerFactoryBeanCustomizer>> customizers,
8078
ObjectProvider<Executor> taskExecutor, ObjectProvider<JobDetail[]> jobDetails,
8179
ObjectProvider<Map<String, Calendar>> calendars,
82-
ObjectProvider<Trigger[]> triggers) {
80+
ObjectProvider<Trigger[]> triggers,
81+
ApplicationContext applicationContext) {
8382
this.properties = properties;
8483
this.customizers = customizers.getIfAvailable();
8584
this.taskExecutor = taskExecutor.getIfAvailable();
8685
this.jobDetails = jobDetails.getIfAvailable();
8786
this.calendars = calendars.getIfAvailable();
8887
this.triggers = triggers.getIfAvailable();
88+
this.applicationContext = applicationContext;
8989
}
9090

9191
@Bean
92-
@ConditionalOnBean(DataSource.class)
92+
@ConditionalOnSingleCandidate(DataSource.class)
9393
@ConditionalOnMissingBean
9494
public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource,
9595
ResourceLoader resourceLoader) {
@@ -98,7 +98,7 @@ public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource
9898

9999
@Bean
100100
@ConditionalOnMissingBean
101-
public SchedulerFactoryBean schedulerFactoryBean() {
101+
public SchedulerFactoryBean quartzScheduler() {
102102
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
103103
schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory(
104104
this.applicationContext.getAutowireCapableBeanFactory()));
@@ -122,12 +122,6 @@ public SchedulerFactoryBean schedulerFactoryBean() {
122122
return schedulerFactoryBean;
123123
}
124124

125-
@Override
126-
public void setApplicationContext(ApplicationContext applicationContext)
127-
throws BeansException {
128-
this.applicationContext = applicationContext;
129-
}
130-
131125
private Properties asProperties(Map<String, String> source) {
132126
Properties properties = new Properties();
133127
properties.putAll(source);
@@ -136,23 +130,29 @@ private Properties asProperties(Map<String, String> source) {
136130

137131
private void customize(SchedulerFactoryBean schedulerFactoryBean) {
138132
if (this.customizers != null) {
139-
AnnotationAwareOrderComparator.sort(this.customizers);
140133
for (SchedulerFactoryBeanCustomizer customizer : this.customizers) {
141134
customizer.customize(schedulerFactoryBean);
142135
}
143136
}
144137
}
145138

146139
@Configuration
147-
@ConditionalOnBean(DataSource.class)
148-
protected static class QuartzSchedulerDataSourceConfiguration {
140+
@ConditionalOnSingleCandidate(DataSource.class)
141+
protected static class JdbcStoreTypeConfiguration {
149142

150143
@Bean
151-
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(DataSource dataSource,
152-
PlatformTransactionManager transactionManager) {
144+
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
145+
QuartzProperties properties, DataSource dataSource,
146+
ObjectProvider<PlatformTransactionManager> transactionManager) {
153147
return schedulerFactoryBean -> {
154-
schedulerFactoryBean.setDataSource(dataSource);
155-
schedulerFactoryBean.setTransactionManager(transactionManager);
148+
if (properties.getJobStoreType() == JobStoreType.JDBC) {
149+
schedulerFactoryBean.setDataSource(dataSource);
150+
PlatformTransactionManager txManager =
151+
transactionManager.getIfUnique();
152+
if (txManager != null) {
153+
schedulerFactoryBean.setTransactionManager(txManager);
154+
}
155+
}
156156
};
157157
}
158158

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDatabaseInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public QuartzDatabaseInitializer(DataSource dataSource, ResourceLoader resourceL
4141

4242
@Override
4343
protected boolean isEnabled() {
44-
return this.properties.getInitializer().isEnabled();
44+
return this.properties.getJdbc().isInitializeSchema();
4545
}
4646

4747
@Override
4848
protected String getSchemaLocation() {
49-
return this.properties.getSchema();
49+
return this.properties.getJdbc().getSchema();
5050
}
5151

5252
@Override

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,69 @@
2525
* Configuration properties for the Quartz Scheduler integration.
2626
*
2727
* @author Vedran Pavic
28+
* @author Stephane Nicoll
2829
* @since 2.0.0
2930
*/
3031
@ConfigurationProperties("spring.quartz")
3132
public class QuartzProperties {
3233

33-
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/quartz/impl/"
34-
+ "jdbcjobstore/tables_@@platform@@.sql";
35-
36-
private final Initializer initializer = new Initializer();
37-
3834
/**
39-
* Additional Quartz Scheduler properties.
35+
* Quartz job store type.
4036
*/
41-
private Map<String, String> properties = new HashMap<>();
37+
private JobStoreType jobStoreType = JobStoreType.MEMORY;
4238

4339
/**
44-
* Path to the SQL file to use to initialize the database schema.
40+
* Additional Quartz Scheduler properties.
4541
*/
46-
private String schema = DEFAULT_SCHEMA_LOCATION;
42+
private final Map<String, String> properties = new HashMap<>();
4743

48-
public Initializer getInitializer() {
49-
return this.initializer;
44+
private final Jdbc jdbc = new Jdbc();
45+
46+
public JobStoreType getJobStoreType() {
47+
return this.jobStoreType;
48+
}
49+
50+
public void setJobStoreType(JobStoreType jobStoreType) {
51+
this.jobStoreType = jobStoreType;
5052
}
5153

5254
public Map<String, String> getProperties() {
5355
return this.properties;
5456
}
5557

56-
public void setProperties(Map<String, String> properties) {
57-
this.properties = properties;
58+
public Jdbc getJdbc() {
59+
return this.jdbc;
5860
}
5961

60-
public String getSchema() {
61-
return this.schema;
62-
}
62+
public static class Jdbc {
6363

64-
public void setSchema(String schema) {
65-
this.schema = schema;
66-
}
64+
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/quartz/impl/"
65+
+ "jdbcjobstore/tables_@@platform@@.sql";
6766

68-
public class Initializer {
67+
/**
68+
* Path to the SQL file to use to initialize the database schema.
69+
*/
70+
private String schema = DEFAULT_SCHEMA_LOCATION;
6971

7072
/**
71-
* Create the required Quartz Scheduler tables on startup if necessary. Enabled
72-
* automatically if the schema is configured.
73+
* Create the required Quartz Scheduler tables on startup.
7374
*/
74-
private boolean enabled = true;
75+
private boolean initializeSchema;
76+
77+
public String getSchema() {
78+
return this.schema;
79+
}
80+
81+
public void setSchema(String schema) {
82+
this.schema = schema;
83+
}
7584

76-
public boolean isEnabled() {
77-
return this.enabled && QuartzProperties.this.getSchema() != null;
85+
public boolean isInitializeSchema() {
86+
return this.initializeSchema;
7887
}
7988

80-
public void setEnabled(boolean enabled) {
81-
this.enabled = enabled;
89+
public void setInitializeSchema(boolean initializeSchema) {
90+
this.initializeSchema = initializeSchema;
8291
}
8392

8493
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/SchedulerFactoryBeanCustomizer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
*/
2929
public interface SchedulerFactoryBeanCustomizer {
3030

31+
/**
32+
* Customize the {@link SchedulerFactoryBean}.
33+
* @param schedulerFactoryBean the scheduler to customize
34+
*/
3135
void customize(SchedulerFactoryBean schedulerFactoryBean);
3236

3337
}

spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@
357357
"name": "spring.mvc.locale-resolver",
358358
"defaultValue": "accept-header"
359359
},
360+
{
361+
"name": "spring.quartz.job-store-type",
362+
"defaultValue": "memory"
363+
},
360364
{
361365
"name": "spring.rabbitmq.cache.connection.mode",
362366
"defaultValue": "channel"

0 commit comments

Comments
 (0)