|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.quartz; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Properties; |
| 22 | +import java.util.concurrent.Executor; |
| 23 | + |
| 24 | +import javax.sql.DataSource; |
| 25 | + |
| 26 | +import org.quartz.Calendar; |
| 27 | +import org.quartz.JobDetail; |
| 28 | +import org.quartz.Scheduler; |
| 29 | +import org.quartz.Trigger; |
| 30 | + |
| 31 | +import org.springframework.beans.BeansException; |
| 32 | +import org.springframework.beans.factory.ObjectProvider; |
| 33 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 34 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 36 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 37 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 38 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 39 | +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; |
| 40 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 41 | +import org.springframework.context.ApplicationContext; |
| 42 | +import org.springframework.context.ApplicationContextAware; |
| 43 | +import org.springframework.context.annotation.Bean; |
| 44 | +import org.springframework.context.annotation.Configuration; |
| 45 | +import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
| 46 | +import org.springframework.core.io.ResourceLoader; |
| 47 | +import org.springframework.scheduling.quartz.SchedulerFactoryBean; |
| 48 | +import org.springframework.transaction.PlatformTransactionManager; |
| 49 | + |
| 50 | +/** |
| 51 | + * {@link EnableAutoConfiguration Auto-configuration} for Quartz Scheduler. |
| 52 | + * |
| 53 | + * @author Vedran Pavic |
| 54 | + * @since 2.0.0 |
| 55 | + */ |
| 56 | +@Configuration |
| 57 | +@ConditionalOnClass({ Scheduler.class, SchedulerFactoryBean.class, PlatformTransactionManager.class }) |
| 58 | +@EnableConfigurationProperties(QuartzProperties.class) |
| 59 | +@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) |
| 60 | +public class QuartzAutoConfiguration implements ApplicationContextAware { |
| 61 | + |
| 62 | + private final QuartzProperties properties; |
| 63 | + |
| 64 | + private final List<SchedulerFactoryBeanCustomizer> customizers; |
| 65 | + |
| 66 | + private final Executor taskExecutor; |
| 67 | + |
| 68 | + private final JobDetail[] jobDetails; |
| 69 | + |
| 70 | + private final Map<String, Calendar> calendars; |
| 71 | + |
| 72 | + private final Trigger[] triggers; |
| 73 | + |
| 74 | + private ApplicationContext applicationContext; |
| 75 | + |
| 76 | + public QuartzAutoConfiguration(QuartzProperties properties, |
| 77 | + ObjectProvider<List<SchedulerFactoryBeanCustomizer>> customizers, |
| 78 | + ObjectProvider<Executor> taskExecutor, ObjectProvider<JobDetail[]> jobDetails, |
| 79 | + ObjectProvider<Map<String, Calendar>> calendars, |
| 80 | + ObjectProvider<Trigger[]> triggers) { |
| 81 | + this.properties = properties; |
| 82 | + this.customizers = customizers.getIfAvailable(); |
| 83 | + this.taskExecutor = taskExecutor.getIfAvailable(); |
| 84 | + this.jobDetails = jobDetails.getIfAvailable(); |
| 85 | + this.calendars = calendars.getIfAvailable(); |
| 86 | + this.triggers = triggers.getIfAvailable(); |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + @ConditionalOnBean(DataSource.class) |
| 91 | + @ConditionalOnMissingBean |
| 92 | + public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource, |
| 93 | + ResourceLoader resourceLoader) { |
| 94 | + return new QuartzDatabaseInitializer(dataSource, resourceLoader, this.properties); |
| 95 | + } |
| 96 | + |
| 97 | + @Bean |
| 98 | + @ConditionalOnMissingBean |
| 99 | + public SchedulerFactoryBean schedulerFactoryBean() { |
| 100 | + SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); |
| 101 | + schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory( |
| 102 | + this.applicationContext.getAutowireCapableBeanFactory())); |
| 103 | + if (!this.properties.getProperties().isEmpty()) { |
| 104 | + schedulerFactoryBean.setQuartzProperties( |
| 105 | + asProperties(this.properties.getProperties())); |
| 106 | + } |
| 107 | + if (this.taskExecutor != null) { |
| 108 | + schedulerFactoryBean.setTaskExecutor(this.taskExecutor); |
| 109 | + } |
| 110 | + if (this.jobDetails != null && this.jobDetails.length > 0) { |
| 111 | + schedulerFactoryBean.setJobDetails(this.jobDetails); |
| 112 | + } |
| 113 | + if (this.calendars != null && !this.calendars.isEmpty()) { |
| 114 | + schedulerFactoryBean.setCalendars(this.calendars); |
| 115 | + } |
| 116 | + if (this.triggers != null && this.triggers.length > 0) { |
| 117 | + schedulerFactoryBean.setTriggers(this.triggers); |
| 118 | + } |
| 119 | + customize(schedulerFactoryBean); |
| 120 | + return schedulerFactoryBean; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void setApplicationContext(ApplicationContext applicationContext) |
| 125 | + throws BeansException { |
| 126 | + this.applicationContext = applicationContext; |
| 127 | + } |
| 128 | + |
| 129 | + private Properties asProperties(Map<String, String> source) { |
| 130 | + Properties properties = new Properties(); |
| 131 | + properties.putAll(source); |
| 132 | + return properties; |
| 133 | + } |
| 134 | + |
| 135 | + private void customize(SchedulerFactoryBean schedulerFactoryBean) { |
| 136 | + if (this.customizers != null) { |
| 137 | + AnnotationAwareOrderComparator.sort(this.customizers); |
| 138 | + for (SchedulerFactoryBeanCustomizer customizer : this.customizers) { |
| 139 | + customizer.customize(schedulerFactoryBean); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Configuration |
| 145 | + @ConditionalOnBean(DataSource.class) |
| 146 | + protected static class QuartzSchedulerDataSourceConfiguration { |
| 147 | + |
| 148 | + @Bean |
| 149 | + public SchedulerFactoryBeanCustomizer dataSourceCustomizer(DataSource dataSource, |
| 150 | + PlatformTransactionManager transactionManager) { |
| 151 | + return schedulerFactoryBean -> { |
| 152 | + schedulerFactoryBean.setDataSource(dataSource); |
| 153 | + schedulerFactoryBean.setTransactionManager(transactionManager); |
| 154 | + }; |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments