Commit 7f420d12 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish contribution

Closes gh-9411
parent db060c84
...@@ -29,11 +29,11 @@ import org.quartz.Scheduler; ...@@ -29,11 +29,11 @@ import org.quartz.Scheduler;
import org.quartz.Trigger; import org.quartz.Trigger;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
...@@ -130,12 +130,22 @@ public class QuartzAutoConfiguration { ...@@ -130,12 +130,22 @@ public class QuartzAutoConfiguration {
@Configuration @Configuration
@ConditionalOnSingleCandidate(DataSource.class) @ConditionalOnSingleCandidate(DataSource.class)
@ConditionalOnProperty(prefix = "spring.quartz", name = "job-store-type", havingValue = "jdbc")
protected static class JdbcStoreTypeConfiguration { protected static class JdbcStoreTypeConfiguration {
@Bean @Bean
public static InitializerSchedulerDependencyPostProcessor initializerSchedulerDependencyPostProcessor() { public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
return new InitializerSchedulerDependencyPostProcessor(); QuartzProperties properties, DataSource dataSource,
ObjectProvider<PlatformTransactionManager> transactionManager) {
return schedulerFactoryBean -> {
if (properties.getJobStoreType() == JobStoreType.JDBC) {
schedulerFactoryBean.setDataSource(dataSource);
PlatformTransactionManager txManager = transactionManager
.getIfUnique();
if (txManager != null) {
schedulerFactoryBean.setTransactionManager(txManager);
}
}
};
} }
@Bean @Bean
...@@ -146,22 +156,16 @@ public class QuartzAutoConfiguration { ...@@ -146,22 +156,16 @@ public class QuartzAutoConfiguration {
} }
@Bean @Bean
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(DataSource dataSource, public static DatabaseInitializerSchedulerDependencyPostProcessor databaseInitializerSchedulerDependencyPostProcessor() {
ObjectProvider<PlatformTransactionManager> transactionManager) { return new DatabaseInitializerSchedulerDependencyPostProcessor();
return schedulerFactoryBean -> {
schedulerFactoryBean.setDataSource(dataSource);
PlatformTransactionManager txManager = transactionManager.getIfUnique();
if (txManager != null) {
schedulerFactoryBean.setTransactionManager(txManager);
}
};
} }
private static class InitializerSchedulerDependencyPostProcessor private static class DatabaseInitializerSchedulerDependencyPostProcessor
extends SchedulerDependsOnPostProcessor { extends AbstractDependsOnBeanFactoryPostProcessor {
InitializerSchedulerDependencyPostProcessor() { DatabaseInitializerSchedulerDependencyPostProcessor() {
super("quartzDatabaseInitializer"); super(Scheduler.class, SchedulerFactoryBean.class,
"quartzDatabaseInitializer");
} }
} }
......
/*
* Copyright 2012-2017 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 org.quartz.Scheduler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
* {@link BeanFactoryPostProcessor} that can be used to dynamically declare that all
* {@link Scheduler} beans should "depend on" one or more specific beans.
*
* @author Vedran Pavic
* @since 2.0.0
* @see BeanDefinition#setDependsOn(String[])
*/
public class SchedulerDependsOnPostProcessor
extends AbstractDependsOnBeanFactoryPostProcessor {
public SchedulerDependsOnPostProcessor(String... dependsOn) {
super(Scheduler.class, SchedulerFactoryBean.class, dependsOn);
}
}
...@@ -50,11 +50,13 @@ import org.springframework.context.ConfigurableApplicationContext; ...@@ -50,11 +50,13 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.quartz.LocalDataSourceJobStore; import org.springframework.scheduling.quartz.LocalDataSourceJobStore;
import org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool; import org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool;
import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -199,13 +201,10 @@ public class QuartzAutoConfigurationTests { ...@@ -199,13 +201,10 @@ public class QuartzAutoConfigurationTests {
this.context = ctx; this.context = ctx;
} }
@Import(ComponentThatUsesScheduler.class)
@Configuration
protected static class BaseQuartzConfiguration { protected static class BaseQuartzConfiguration {
@Bean
public ComponentThatUsesScheduler component() {
return new ComponentThatUsesScheduler();
}
} }
@Configuration @Configuration
...@@ -283,9 +282,13 @@ public class QuartzAutoConfigurationTests { ...@@ -283,9 +282,13 @@ public class QuartzAutoConfigurationTests {
public static class ComponentThatUsesScheduler { public static class ComponentThatUsesScheduler {
@Autowired
private Scheduler scheduler; private Scheduler scheduler;
public ComponentThatUsesScheduler(Scheduler scheduler) {
Assert.notNull(scheduler, "Scheduler must not be null");
this.scheduler = scheduler;
}
} }
public static class FooJob extends QuartzJobBean { public static class FooJob extends QuartzJobBean {
......
...@@ -23,14 +23,6 @@ ...@@ -23,14 +23,6 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId> <artifactId>spring-boot-starter-quartz</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
...@@ -18,36 +18,21 @@ package sample.quartz; ...@@ -18,36 +18,21 @@ package sample.quartz;
import org.quartz.JobBuilder; import org.quartz.JobBuilder;
import org.quartz.JobDetail; import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger; import org.quartz.Trigger;
import org.quartz.TriggerBuilder; import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@SpringBootApplication @SpringBootApplication
public class SampleQuartzApplication implements CommandLineRunner { public class SampleQuartzApplication {
@Autowired
private Scheduler scheduler;
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SampleQuartzApplication.class, args); SpringApplication.run(SampleQuartzApplication.class, args);
} }
@Override
public void run(String... args) throws Exception {
Trigger trigger = TriggerBuilder.newTrigger().forJob(sampleJobDetail())
.withIdentity("startTrigger").usingJobData("name", "Boot").startNow()
.build();
this.scheduler.scheduleJob(trigger);
}
@Bean @Bean
public JobDetail sampleJobDetail() { public JobDetail sampleJobDetail() {
return JobBuilder.newJob(SampleJob.class).withIdentity("sampleJob") return JobBuilder.newJob(SampleJob.class).withIdentity("sampleJob")
......
spring.quartz.job-store-type=jdbc
spring.quartz.jdbc.initialize-schema=true
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment