Skip to content

Commit 10cbae2

Browse files
committed
Add Quartz Scheduler support
1 parent 2a6b1e6 commit 10cbae2

File tree

21 files changed

+1022
-1
lines changed

21 files changed

+1022
-1
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,11 @@
592592
<artifactId>narayana-jts-integration</artifactId>
593593
<optional>true</optional>
594594
</dependency>
595+
<dependency>
596+
<groupId>org.quartz-scheduler</groupId>
597+
<artifactId>quartz</artifactId>
598+
<optional>true</optional>
599+
</dependency>
595600
<!-- Annotation processing -->
596601
<dependency>
597602
<groupId>org.springframework.boot</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright 2012-2016 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+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Properties;
22+
import java.util.concurrent.Executor;
23+
24+
import javax.sql.DataSource;
25+
26+
import org.quartz.Calendar;
27+
import org.quartz.JobDetail;
28+
import org.quartz.Scheduler;
29+
import org.quartz.Trigger;
30+
31+
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
33+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
34+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
35+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
36+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
37+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
38+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
39+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
40+
import org.springframework.context.annotation.Bean;
41+
import org.springframework.context.annotation.Configuration;
42+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
43+
import org.springframework.core.io.ResourceLoader;
44+
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
45+
import org.springframework.transaction.PlatformTransactionManager;
46+
47+
/**
48+
* {@link EnableAutoConfiguration Auto-configuration} for Quartz Scheduler.
49+
*
50+
* @author Vedran Pavic
51+
* @since 1.4.0
52+
*/
53+
@Configuration
54+
@ConditionalOnClass({ Scheduler.class, SchedulerFactoryBean.class, PlatformTransactionManager.class })
55+
@EnableConfigurationProperties(QuartzProperties.class)
56+
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
57+
public class QuartzAutoConfiguration {
58+
59+
private static final String APPLICATION_CONTEXT_KEY = "applicationContext";
60+
61+
@Autowired
62+
private QuartzProperties properties;
63+
64+
@Autowired(required = false)
65+
private List<QuartzSchedulerFactoryBeanCustomizer> customizers;
66+
67+
@Autowired(required = false)
68+
private Executor taskExecutor;
69+
70+
@Autowired(required = false)
71+
private JobDetail[] jobDetails;
72+
73+
@Autowired(required = false)
74+
private Map<String, Calendar> calendars;
75+
76+
@Autowired(required = false)
77+
private Trigger[] triggers;
78+
79+
@Bean
80+
@ConditionalOnMissingBean
81+
@ConditionalOnBean(DataSource.class)
82+
public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource,
83+
ResourceLoader resourceLoader) {
84+
return new QuartzDatabaseInitializer(this.properties, dataSource, resourceLoader);
85+
}
86+
87+
@Bean
88+
@ConditionalOnMissingBean
89+
public SchedulerFactoryBean schedulerFactoryBean() {
90+
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
91+
schedulerFactoryBean.setApplicationContextSchedulerContextKey(
92+
APPLICATION_CONTEXT_KEY);
93+
if (!this.properties.getProperties().isEmpty()) {
94+
schedulerFactoryBean.setQuartzProperties(
95+
asProperties(this.properties.getProperties()));
96+
}
97+
if (this.taskExecutor != null) {
98+
schedulerFactoryBean.setTaskExecutor(this.taskExecutor);
99+
}
100+
if (this.jobDetails != null && this.jobDetails.length > 0) {
101+
schedulerFactoryBean.setJobDetails(this.jobDetails);
102+
}
103+
if (this.calendars != null && !this.calendars.isEmpty()) {
104+
schedulerFactoryBean.setCalendars(this.calendars);
105+
}
106+
if (this.triggers != null && this.triggers.length > 0) {
107+
schedulerFactoryBean.setTriggers(this.triggers);
108+
}
109+
customize(schedulerFactoryBean);
110+
return schedulerFactoryBean;
111+
}
112+
113+
@Bean
114+
@ConditionalOnBean(DataSource.class)
115+
public QuartzSchedulerFactoryBeanCustomizer dataSourceCustomizer(
116+
final DataSource dataSource, final PlatformTransactionManager transactionManager) {
117+
return new QuartzSchedulerFactoryBeanCustomizer() {
118+
119+
@Override
120+
public void customize(SchedulerFactoryBean schedulerFactoryBean) {
121+
schedulerFactoryBean.setDataSource(dataSource);
122+
schedulerFactoryBean.setTransactionManager(transactionManager);
123+
}
124+
125+
};
126+
}
127+
128+
private Properties asProperties(Map<String, String> source) {
129+
Properties properties = new Properties();
130+
properties.putAll(source);
131+
return properties;
132+
}
133+
134+
private void customize(SchedulerFactoryBean schedulerFactoryBean) {
135+
if (this.customizers != null) {
136+
AnnotationAwareOrderComparator.sort(this.customizers);
137+
for (QuartzSchedulerFactoryBeanCustomizer customizer : this.customizers) {
138+
customizer.customize(schedulerFactoryBean);
139+
}
140+
}
141+
}
142+
143+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2016 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+
import javax.annotation.PostConstruct;
20+
import javax.sql.DataSource;
21+
22+
import org.springframework.core.io.ResourceLoader;
23+
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
24+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* Initialize the Quartz Scheduler schema (ignoring errors).
29+
*
30+
* @author Vedran Pavic
31+
* @since 1.4.0
32+
*/
33+
public class QuartzDatabaseInitializer {
34+
35+
private QuartzProperties properties;
36+
37+
private DataSource dataSource;
38+
39+
private ResourceLoader resourceLoader;
40+
41+
/**
42+
* Create a new {@link QuartzDatabaseInitializer}.
43+
* @param properties the quartz configuration properties
44+
* @param dataSource the data source
45+
* @param resourceLoader the resource loader
46+
*/
47+
public QuartzDatabaseInitializer(QuartzProperties properties,
48+
DataSource dataSource, ResourceLoader resourceLoader) {
49+
Assert.notNull(properties, "QuartzProperties must not be null");
50+
Assert.notNull(dataSource, "DataSource must not be null");
51+
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
52+
this.properties = properties;
53+
this.dataSource = dataSource;
54+
this.resourceLoader = resourceLoader;
55+
}
56+
57+
@PostConstruct
58+
protected void initialize() {
59+
if (this.properties.getInitializer().isEnabled()) {
60+
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
61+
populator.addScript(this.resourceLoader.getResource(
62+
this.properties.getSchema()));
63+
populator.setContinueOnError(true);
64+
DatabasePopulatorUtils.execute(populator, this.dataSource);
65+
}
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2016 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+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
24+
/**
25+
* Configuration properties for the Quartz Scheduler integration.
26+
*
27+
* @author Vedran Pavic
28+
* @since 1.4.0
29+
*/
30+
@ConfigurationProperties("spring.quartz")
31+
public class QuartzProperties {
32+
33+
/**
34+
* Path to the SQL file to use to initialize the database schema.
35+
*/
36+
private String schema = "classpath:db/schema-quartz.sql";
37+
38+
/**
39+
* Additional Quartz Scheduler properties.
40+
*/
41+
private Map<String, String> properties = new HashMap<String, String>();
42+
43+
private Initializer initializer = new Initializer();
44+
45+
public String getSchema() {
46+
return this.schema;
47+
}
48+
49+
public void setSchema(String schema) {
50+
this.schema = schema;
51+
}
52+
53+
public Map<String, String> getProperties() {
54+
return this.properties;
55+
}
56+
57+
public void setProperties(Map<String, String> properties) {
58+
this.properties = properties;
59+
}
60+
61+
public Initializer getInitializer() {
62+
return this.initializer;
63+
}
64+
65+
public static class Initializer {
66+
67+
/**
68+
* Create the required Quartz tables on startup if necessary.
69+
*/
70+
private boolean enabled = true;
71+
72+
public boolean isEnabled() {
73+
return this.enabled;
74+
}
75+
76+
public void setEnabled(boolean enabled) {
77+
this.enabled = enabled;
78+
}
79+
80+
}
81+
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2016 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+
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
20+
21+
/**
22+
* Callback interface that can be implemented by beans wishing to customize the Quartz
23+
* SchedulerFactoryBean before it is fully initialized, in particular to tune its
24+
* configuration.
25+
*
26+
* @author Vedran Pavic
27+
* @since 1.4.0
28+
*/
29+
public interface QuartzSchedulerFactoryBeanCustomizer {
30+
31+
void customize(SchedulerFactoryBean schedulerFactoryBean);
32+
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2016 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+
/**
18+
* Auto-configuration for Quartz Scheduler.
19+
*/
20+
package org.springframework.boot.autoconfigure.quartz;

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
7171
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
7272
org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration,\
7373
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
74+
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
7475
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
7576
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
7677
org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\

0 commit comments

Comments
 (0)