Merge branch '2.2.x' into 2.3.x

Closes gh-24014
This commit is contained in:
Andy Wilkinson 2020-11-03 16:38:33 +00:00
commit f9ff39a7a6
5 changed files with 115 additions and 3 deletions

View File

@ -106,11 +106,13 @@ public class QuartzAutoConfiguration {
@Order(0) @Order(0)
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(QuartzProperties properties, DataSource dataSource, public SchedulerFactoryBeanCustomizer dataSourceCustomizer(QuartzProperties properties, DataSource dataSource,
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource, @QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
ObjectProvider<PlatformTransactionManager> transactionManager) { ObjectProvider<PlatformTransactionManager> transactionManager,
@QuartzTransactionManager ObjectProvider<PlatformTransactionManager> quartzTransactionManager) {
return (schedulerFactoryBean) -> { return (schedulerFactoryBean) -> {
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource); DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
schedulerFactoryBean.setDataSource(dataSourceToUse); schedulerFactoryBean.setDataSource(dataSourceToUse);
PlatformTransactionManager txManager = transactionManager.getIfUnique(); PlatformTransactionManager txManager = getTransactionManager(transactionManager,
quartzTransactionManager);
if (txManager != null) { if (txManager != null) {
schedulerFactoryBean.setTransactionManager(txManager); schedulerFactoryBean.setTransactionManager(txManager);
} }
@ -122,6 +124,14 @@ public class QuartzAutoConfiguration {
return (dataSourceIfAvailable != null) ? dataSourceIfAvailable : dataSource; return (dataSourceIfAvailable != null) ? dataSourceIfAvailable : dataSource;
} }
private PlatformTransactionManager getTransactionManager(
ObjectProvider<PlatformTransactionManager> transactionManager,
ObjectProvider<PlatformTransactionManager> quartzTransactionManager) {
PlatformTransactionManager transactionManagerIfAvailable = quartzTransactionManager.getIfAvailable();
return (transactionManagerIfAvailable != null) ? transactionManagerIfAvailable
: transactionManager.getIfUnique();
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public QuartzDataSourceInitializer quartzDataSourceInitializer(DataSource dataSource, public QuartzDataSourceInitializer quartzDataSourceInitializer(DataSource dataSource,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
* {@code @Primary}. * {@code @Primary}.
* *
* @author Madhura Bhave * @author Madhura Bhave
* @see QuartzDataSource
* @since 2.0.2 * @since 2.0.2
*/ */
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE }) @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })

View File

@ -0,0 +1,42 @@
/*
* Copyright 2012-2020 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
*
* https://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 TransactionManager to be injected into Quartz
* auto-configuration. Can be used on a secondary transaction manager, if there is another
* one marked as {@code @Primary}.
*
* @author Andy Wilkinson
* @see QuartzDataSource
* @since 2.2.11
*/
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Qualifier
public @interface QuartzTransactionManager {
}

View File

@ -60,9 +60,11 @@ import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.quartz.LocalDataSourceJobStore; import org.springframework.scheduling.quartz.LocalDataSourceJobStore;
import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -136,6 +138,17 @@ class QuartzAutoConfigurationTests {
.run(assertDataSourceJobStore("quartzDataSource")); .run(assertDataSourceJobStore("quartzDataSource"));
} }
@Test
void transactionManagerWithQuartzTransactionManagerUsedWhenMultiplePresent() {
this.contextRunner
.withUserConfiguration(QuartzJobsConfiguration.class, MultipleTransactionManagersConfiguration.class)
.withPropertyValues("spring.quartz.job-store-type=jdbc").run((context) -> {
SchedulerFactoryBean schedulerFactoryBean = context.getBean(SchedulerFactoryBean.class);
assertThat(schedulerFactoryBean).extracting("transactionManager")
.isEqualTo(context.getBean("quartzTransactionManager"));
});
}
private ContextConsumer<AssertableApplicationContext> assertDataSourceJobStore(String datasourceName) { private ContextConsumer<AssertableApplicationContext> assertDataSourceJobStore(String datasourceName) {
return (context) -> { return (context) -> {
assertThat(context).hasSingleBean(Scheduler.class); assertThat(context).hasSingleBean(Scheduler.class);
@ -431,6 +444,51 @@ class QuartzAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class MultipleTransactionManagersConfiguration extends BaseQuartzConfiguration {
private final DataSource primaryDataSource = createTestDataSource();
private final DataSource quartzDataSource = createTestDataSource();
@Bean
@Primary
DataSource applicationDataSource() {
return this.primaryDataSource;
}
@Bean
@QuartzDataSource
DataSource quartzDataSource() {
return this.quartzDataSource;
}
@Bean
@Primary
PlatformTransactionManager applicationTransactionManager() {
return new DataSourceTransactionManager(this.primaryDataSource);
}
@Bean
@QuartzTransactionManager
PlatformTransactionManager quartzTransactionManager() {
return new DataSourceTransactionManager(this.quartzDataSource);
}
private DataSource createTestDataSource() {
DataSourceProperties properties = new DataSourceProperties();
properties.setGenerateUniqueName(true);
try {
properties.afterPropertiesSet();
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
return properties.initializeDataSourceBuilder().build();
}
}
static class ComponentThatUsesScheduler { static class ComponentThatUsesScheduler {
ComponentThatUsesScheduler(Scheduler scheduler) { ComponentThatUsesScheduler(Scheduler scheduler) {

View File

@ -6099,6 +6099,7 @@ It is also possible to provide a custom script by setting the configprop:spring.
To have Quartz use a `DataSource` other than the application's main `DataSource`, declare a `DataSource` bean, annotating its `@Bean` method with `@QuartzDataSource`. To have Quartz use a `DataSource` other than the application's main `DataSource`, declare a `DataSource` bean, annotating its `@Bean` method with `@QuartzDataSource`.
Doing so ensures that the Quartz-specific `DataSource` is used by both the `SchedulerFactoryBean` and for schema initialization. Doing so ensures that the Quartz-specific `DataSource` is used by both the `SchedulerFactoryBean` and for schema initialization.
Similarly, to have Quartz use a `TransactionManager` other than the application's main `TransactionManager` declare a `TransactionManager` bean, annotating its `@Bean` method with `@QuartzTransactionManager`.
By default, jobs created by configuration will not overwrite already registered jobs that have been read from a persistent job store. By default, jobs created by configuration will not overwrite already registered jobs that have been read from a persistent job store.
To enable overwriting existing job definitions set the configprop:spring.quartz.overwrite-existing-jobs[] property. To enable overwriting existing job definitions set the configprop:spring.quartz.overwrite-existing-jobs[] property.