Skip to content

Commit 2f5ab50

Browse files
committed
Add @QuartzDataSource for quartz auto-configuration
Closes spring-projectsgh-12755
1 parent 5587eac commit 2f5ab50

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ protected static class JdbcStoreTypeConfiguration {
135135
@Bean
136136
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
137137
QuartzProperties properties, DataSource dataSource,
138+
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
138139
ObjectProvider<PlatformTransactionManager> transactionManager) {
139140
return (schedulerFactoryBean) -> {
140141
if (properties.getJobStoreType() == JobStoreType.JDBC) {
141-
schedulerFactoryBean.setDataSource(dataSource);
142+
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
143+
schedulerFactoryBean.setDataSource(dataSourceToUse);
142144
PlatformTransactionManager txManager = transactionManager
143145
.getIfUnique();
144146
if (txManager != null) {
@@ -148,12 +150,20 @@ public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
148150
};
149151
}
150152

153+
private DataSource getDataSource(DataSource dataSource,
154+
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource) {
155+
DataSource dataSourceIfAvailable = quartzDataSource.getIfAvailable();
156+
return (dataSourceIfAvailable != null ? dataSourceIfAvailable : dataSource);
157+
}
158+
151159
@Bean
152160
@ConditionalOnMissingBean
153161
public QuartzDataSourceInitializer quartzDataSourceInitializer(
154-
DataSource dataSource, ResourceLoader resourceLoader,
162+
DataSource dataSource, @QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
163+
ResourceLoader resourceLoader,
155164
QuartzProperties properties) {
156-
return new QuartzDataSourceInitializer(dataSource, resourceLoader,
165+
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
166+
return new QuartzDataSourceInitializer(dataSourceToUse, resourceLoader,
157167
properties);
158168
}
159169

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2018 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.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.beans.factory.annotation.Qualifier;
26+
27+
/**
28+
* Qualifier annotation for a DataSource to be injected into Quartz auto-configuration. Can be used on
29+
* a secondary data source, if there is another one marked as {@code @Primary}.
30+
*
31+
* @author Madhura Bhave
32+
* @since 2.0.2
33+
*/
34+
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE,
35+
ElementType.ANNOTATION_TYPE })
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@Documented
38+
@Qualifier
39+
public @interface QuartzDataSource {
40+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import javax.sql.DataSource;
2323

24+
import com.zaxxer.hikari.HikariDataSource;
2425
import org.junit.After;
2526
import org.junit.Rule;
2627
import org.junit.Test;
@@ -43,6 +44,7 @@
4344
import org.springframework.beans.factory.annotation.Autowired;
4445
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
4546
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
47+
import org.springframework.boot.jdbc.DataSourceBuilder;
4648
import org.springframework.boot.test.rule.OutputCapture;
4749
import org.springframework.boot.test.util.TestPropertyValues;
4850
import org.springframework.context.ConfigurableApplicationContext;
@@ -207,6 +209,12 @@ public void withCustomizer() throws Exception {
207209
assertThat(scheduler.getSchedulerName()).isEqualTo("fooScheduler");
208210
}
209211

212+
@Test
213+
public void dataSourceWithQuartzDataSourceQualifierUsedWhenMultiplePresent() {
214+
load(MultipleDataSourceConfiguration.class,
215+
"spring.quartz.job-store-type=jdbc");
216+
}
217+
210218
private void load(String... environment) {
211219
load(new Class<?>[0], environment);
212220
}
@@ -345,6 +353,25 @@ public SchedulerFactoryBeanCustomizer customizer() {
345353

346354
}
347355

356+
@Configuration
357+
protected static class MultipleDataSourceConfiguration extends BaseQuartzConfiguration {
358+
359+
@Bean
360+
@Primary
361+
public DataSource applicationDataSource() {
362+
return new HikariDataSource();
363+
}
364+
365+
366+
@QuartzDataSource
367+
@Bean
368+
public DataSource quartzDataSource() {
369+
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:quartztest")
370+
.username("sa").build();
371+
}
372+
373+
}
374+
348375
public static class ComponentThatUsesScheduler {
349376

350377
public ComponentThatUsesScheduler(Scheduler scheduler) {

0 commit comments

Comments
 (0)