mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish contribution
Closes gh-9411
This commit is contained in:
parent
db060c847d
commit
7f420d1268
@ -29,11 +29,11 @@ import org.quartz.Scheduler;
|
||||
import org.quartz.Trigger;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
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.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
@ -130,12 +130,22 @@ public class QuartzAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnSingleCandidate(DataSource.class)
|
||||
@ConditionalOnProperty(prefix = "spring.quartz", name = "job-store-type", havingValue = "jdbc")
|
||||
protected static class JdbcStoreTypeConfiguration {
|
||||
|
||||
@Bean
|
||||
public static InitializerSchedulerDependencyPostProcessor initializerSchedulerDependencyPostProcessor() {
|
||||
return new InitializerSchedulerDependencyPostProcessor();
|
||||
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(
|
||||
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
|
||||
@ -146,22 +156,16 @@ public class QuartzAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(DataSource dataSource,
|
||||
ObjectProvider<PlatformTransactionManager> transactionManager) {
|
||||
return schedulerFactoryBean -> {
|
||||
schedulerFactoryBean.setDataSource(dataSource);
|
||||
PlatformTransactionManager txManager = transactionManager.getIfUnique();
|
||||
if (txManager != null) {
|
||||
schedulerFactoryBean.setTransactionManager(txManager);
|
||||
}
|
||||
};
|
||||
public static DatabaseInitializerSchedulerDependencyPostProcessor databaseInitializerSchedulerDependencyPostProcessor() {
|
||||
return new DatabaseInitializerSchedulerDependencyPostProcessor();
|
||||
}
|
||||
|
||||
private static class InitializerSchedulerDependencyPostProcessor
|
||||
extends SchedulerDependsOnPostProcessor {
|
||||
private static class DatabaseInitializerSchedulerDependencyPostProcessor
|
||||
extends AbstractDependsOnBeanFactoryPostProcessor {
|
||||
|
||||
InitializerSchedulerDependencyPostProcessor() {
|
||||
super("quartzDatabaseInitializer");
|
||||
DatabaseInitializerSchedulerDependencyPostProcessor() {
|
||||
super(Scheduler.class, SchedulerFactoryBean.class,
|
||||
"quartzDatabaseInitializer");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.quartz.LocalDataSourceJobStore;
|
||||
import org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@ -199,13 +201,10 @@ public class QuartzAutoConfigurationTests {
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Import(ComponentThatUsesScheduler.class)
|
||||
@Configuration
|
||||
protected static class BaseQuartzConfiguration {
|
||||
|
||||
@Bean
|
||||
public ComponentThatUsesScheduler component() {
|
||||
return new ComponentThatUsesScheduler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ -283,9 +282,13 @@ public class QuartzAutoConfigurationTests {
|
||||
|
||||
public static class ComponentThatUsesScheduler {
|
||||
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
|
||||
public ComponentThatUsesScheduler(Scheduler scheduler) {
|
||||
Assert.notNull(scheduler, "Scheduler must not be null");
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FooJob extends QuartzJobBean {
|
||||
|
@ -23,14 +23,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-quartz</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@ -18,36 +18,21 @@ package sample.quartz;
|
||||
|
||||
import org.quartz.JobBuilder;
|
||||
import org.quartz.JobDetail;
|
||||
import org.quartz.Scheduler;
|
||||
import org.quartz.SimpleScheduleBuilder;
|
||||
import org.quartz.Trigger;
|
||||
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.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleQuartzApplication implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
public class SampleQuartzApplication {
|
||||
|
||||
public static void main(String[] 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
|
||||
public JobDetail sampleJobDetail() {
|
||||
return JobBuilder.newJob(SampleJob.class).withIdentity("sampleJob")
|
||||
|
@ -1,2 +0,0 @@
|
||||
spring.quartz.job-store-type=jdbc
|
||||
spring.quartz.jdbc.initialize-schema=true
|
Loading…
Reference in New Issue
Block a user