Skip to content

Commit

Permalink
Merge branch '2.3.x'
Browse files Browse the repository at this point in the history
Closes gh-24015
  • Loading branch information
wilkinsona committed Nov 3, 2020
2 parents d41e739 + f9ff39a commit 73c4442
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
Expand Up @@ -106,11 +106,13 @@ protected static class JdbcStoreTypeConfiguration {
@Order(0)
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(QuartzProperties properties, DataSource dataSource,
@QuartzDataSource ObjectProvider<DataSource> quartzDataSource,
ObjectProvider<PlatformTransactionManager> transactionManager) {
ObjectProvider<PlatformTransactionManager> transactionManager,
@QuartzTransactionManager ObjectProvider<PlatformTransactionManager> quartzTransactionManager) {
return (schedulerFactoryBean) -> {
DataSource dataSourceToUse = getDataSource(dataSource, quartzDataSource);
schedulerFactoryBean.setDataSource(dataSourceToUse);
PlatformTransactionManager txManager = transactionManager.getIfUnique();
PlatformTransactionManager txManager = getTransactionManager(transactionManager,
quartzTransactionManager);
if (txManager != null) {
schedulerFactoryBean.setTransactionManager(txManager);
}
Expand All @@ -122,6 +124,14 @@ private DataSource getDataSource(DataSource dataSource, ObjectProvider<DataSourc
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
@ConditionalOnMissingBean
public QuartzDataSourceInitializer quartzDataSourceInitializer(DataSource dataSource,
Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
* {@code @Primary}.
*
* @author Madhura Bhave
* @see QuartzDataSource
* @since 2.0.2
*/
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
Expand Down
@@ -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 {

}
Expand Up @@ -60,9 +60,11 @@
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.quartz.LocalDataSourceJobStore;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -136,6 +138,17 @@ void dataSourceWithQuartzDataSourceQualifierUsedWhenMultiplePresent() {
.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) {
return (context) -> {
assertThat(context).hasSingleBean(Scheduler.class);
Expand Down Expand Up @@ -431,6 +444,51 @@ private DataSource createTestDataSource() throws Exception {

}

@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 {

ComponentThatUsesScheduler(Scheduler scheduler) {
Expand Down
Expand Up @@ -6615,6 +6615,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`.
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.
To enable overwriting existing job definitions set the configprop:spring.quartz.overwrite-existing-jobs[] property.
Expand Down

0 comments on commit 73c4442

Please sign in to comment.