Add @QuartzDataSource for quartz auto-configuration
Closes gh-12755
This commit is contained in:
@@ -135,10 +135,12 @@ public class QuartzAutoConfiguration {
|
||||
@Bean
|
||||
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
|
||||
QuartzProperties properties, DataSource dataSource,
|
||||
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
|
||||
ObjectProvider<PlatformTransactionManager> transactionManager) {
|
||||
return (schedulerFactoryBean) -> {
|
||||
if (properties.getJobStoreType() == JobStoreType.JDBC) {
|
||||
schedulerFactoryBean.setDataSource(dataSource);
|
||||
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
|
||||
schedulerFactoryBean.setDataSource(dataSourceToUse);
|
||||
PlatformTransactionManager txManager = transactionManager
|
||||
.getIfUnique();
|
||||
if (txManager != null) {
|
||||
@@ -148,12 +150,20 @@ public class QuartzAutoConfiguration {
|
||||
};
|
||||
}
|
||||
|
||||
private DataSource getDataSource(DataSource dataSource,
|
||||
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource) {
|
||||
DataSource dataSourceIfAvailable = quartzDataSource.getIfAvailable();
|
||||
return (dataSourceIfAvailable != null ? dataSourceIfAvailable : dataSource);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public QuartzDataSourceInitializer quartzDataSourceInitializer(
|
||||
DataSource dataSource, ResourceLoader resourceLoader,
|
||||
DataSource dataSource, @QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
|
||||
ResourceLoader resourceLoader,
|
||||
QuartzProperties properties) {
|
||||
return new QuartzDataSourceInitializer(dataSource, resourceLoader,
|
||||
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
|
||||
return new QuartzDataSourceInitializer(dataSourceToUse, resourceLoader,
|
||||
properties);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
/**
|
||||
* Qualifier annotation for a DataSource to be injected into Quartz auto-configuration. Can be used on
|
||||
* a secondary data source, if there is another one marked as {@code @Primary}.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @since 2.0.2
|
||||
*/
|
||||
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE,
|
||||
ElementType.ANNOTATION_TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Qualifier
|
||||
public @interface QuartzDataSource {
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import java.util.concurrent.Executors;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -43,6 +44,7 @@ import org.quartz.simpl.SimpleThreadPool;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -207,6 +209,12 @@ public class QuartzAutoConfigurationTests {
|
||||
assertThat(scheduler.getSchedulerName()).isEqualTo("fooScheduler");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dataSourceWithQuartzDataSourceQualifierUsedWhenMultiplePresent() {
|
||||
load(MultipleDataSourceConfiguration.class,
|
||||
"spring.quartz.job-store-type=jdbc");
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
load(new Class<?>[0], environment);
|
||||
}
|
||||
@@ -345,6 +353,25 @@ public class QuartzAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class MultipleDataSourceConfiguration extends BaseQuartzConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public DataSource applicationDataSource() {
|
||||
return new HikariDataSource();
|
||||
}
|
||||
|
||||
|
||||
@QuartzDataSource
|
||||
@Bean
|
||||
public DataSource quartzDataSource() {
|
||||
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:quartztest")
|
||||
.username("sa").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class ComponentThatUsesScheduler {
|
||||
|
||||
public ComponentThatUsesScheduler(Scheduler scheduler) {
|
||||
|
||||
Reference in New Issue
Block a user