-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Add Quartz Scheduler support #4299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ain/java/org/springframework/boot/autoconfigure/quartz/AutowireCapableBeanJobFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.spi.TriggerFiredBundle; | ||
|
||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; | ||
import org.springframework.scheduling.quartz.SpringBeanJobFactory; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Subclass of {@link SpringBeanJobFactory} that supports auto-wiring job beans. | ||
* | ||
* @author Vedran Pavic | ||
* @since 2.0.0 | ||
* @see <a href="http://blog.btmatthews.com/?p=40#comment-33797"> Inject application | ||
* context dependencies in Quartz job beans</a> | ||
*/ | ||
class AutowireCapableBeanJobFactory extends SpringBeanJobFactory { | ||
|
||
private final AutowireCapableBeanFactory beanFactory; | ||
|
||
AutowireCapableBeanJobFactory(AutowireCapableBeanFactory beanFactory) { | ||
Assert.notNull(beanFactory, "Bean factory must not be null"); | ||
this.beanFactory = beanFactory; | ||
} | ||
|
||
@Override | ||
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { | ||
Object jobInstance = super.createJobInstance(bundle); | ||
this.beanFactory.autowireBean(jobInstance); | ||
this.beanFactory.initializeBean(jobInstance, null); | ||
return jobInstance; | ||
} | ||
|
||
} |
161 changes: 161 additions & 0 deletions
161
.../src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* 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 java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.Executor; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.quartz.Calendar; | ||
import org.quartz.JobDetail; | ||
import org.quartz.Scheduler; | ||
import org.quartz.Trigger; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.AnnotationAwareOrderComparator; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.scheduling.quartz.SchedulerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Quartz Scheduler. | ||
* | ||
* @author Vedran Pavic | ||
* @since 2.0.0 | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass({ Scheduler.class, SchedulerFactoryBean.class, | ||
PlatformTransactionManager.class }) | ||
@EnableConfigurationProperties(QuartzProperties.class) | ||
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, | ||
HibernateJpaAutoConfiguration.class }) | ||
public class QuartzAutoConfiguration implements ApplicationContextAware { | ||
|
||
private final QuartzProperties properties; | ||
|
||
private final List<SchedulerFactoryBeanCustomizer> customizers; | ||
|
||
private final Executor taskExecutor; | ||
|
||
private final JobDetail[] jobDetails; | ||
|
||
private final Map<String, Calendar> calendars; | ||
|
||
private final Trigger[] triggers; | ||
|
||
private ApplicationContext applicationContext; | ||
|
||
public QuartzAutoConfiguration(QuartzProperties properties, | ||
ObjectProvider<List<SchedulerFactoryBeanCustomizer>> customizers, | ||
ObjectProvider<Executor> taskExecutor, ObjectProvider<JobDetail[]> jobDetails, | ||
ObjectProvider<Map<String, Calendar>> calendars, | ||
ObjectProvider<Trigger[]> triggers) { | ||
this.properties = properties; | ||
this.customizers = customizers.getIfAvailable(); | ||
this.taskExecutor = taskExecutor.getIfAvailable(); | ||
this.jobDetails = jobDetails.getIfAvailable(); | ||
this.calendars = calendars.getIfAvailable(); | ||
this.triggers = triggers.getIfAvailable(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnBean(DataSource.class) | ||
@ConditionalOnMissingBean | ||
public QuartzDatabaseInitializer quartzDatabaseInitializer(DataSource dataSource, | ||
ResourceLoader resourceLoader) { | ||
return new QuartzDatabaseInitializer(dataSource, resourceLoader, this.properties); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public SchedulerFactoryBean schedulerFactoryBean() { | ||
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); | ||
schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory( | ||
this.applicationContext.getAutowireCapableBeanFactory())); | ||
if (!this.properties.getProperties().isEmpty()) { | ||
schedulerFactoryBean | ||
.setQuartzProperties(asProperties(this.properties.getProperties())); | ||
} | ||
if (this.taskExecutor != null) { | ||
schedulerFactoryBean.setTaskExecutor(this.taskExecutor); | ||
} | ||
if (this.jobDetails != null && this.jobDetails.length > 0) { | ||
schedulerFactoryBean.setJobDetails(this.jobDetails); | ||
} | ||
if (this.calendars != null && !this.calendars.isEmpty()) { | ||
schedulerFactoryBean.setCalendars(this.calendars); | ||
} | ||
if (this.triggers != null && this.triggers.length > 0) { | ||
schedulerFactoryBean.setTriggers(this.triggers); | ||
} | ||
customize(schedulerFactoryBean); | ||
return schedulerFactoryBean; | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) | ||
throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
private Properties asProperties(Map<String, String> source) { | ||
Properties properties = new Properties(); | ||
properties.putAll(source); | ||
return properties; | ||
} | ||
|
||
private void customize(SchedulerFactoryBean schedulerFactoryBean) { | ||
if (this.customizers != null) { | ||
AnnotationAwareOrderComparator.sort(this.customizers); | ||
for (SchedulerFactoryBeanCustomizer customizer : this.customizers) { | ||
customizer.customize(schedulerFactoryBean); | ||
} | ||
} | ||
} | ||
|
||
@Configuration | ||
@ConditionalOnBean(DataSource.class) | ||
protected static class QuartzSchedulerDataSourceConfiguration { | ||
|
||
@Bean | ||
public SchedulerFactoryBeanCustomizer dataSourceCustomizer(DataSource dataSource, | ||
PlatformTransactionManager transactionManager) { | ||
return schedulerFactoryBean -> { | ||
schedulerFactoryBean.setDataSource(dataSource); | ||
schedulerFactoryBean.setTransactionManager(transactionManager); | ||
}; | ||
} | ||
|
||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...rc/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDatabaseInitializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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 javax.sql.DataSource; | ||
|
||
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Initializer for Quartz Scheduler schema. | ||
* | ||
* @author Vedran Pavic | ||
* @since 2.0.0 | ||
*/ | ||
public class QuartzDatabaseInitializer extends AbstractDatabaseInitializer { | ||
|
||
private final QuartzProperties properties; | ||
|
||
public QuartzDatabaseInitializer(DataSource dataSource, ResourceLoader resourceLoader, | ||
QuartzProperties properties) { | ||
super(dataSource, resourceLoader); | ||
Assert.notNull(properties, "QuartzProperties must not be null"); | ||
this.properties = properties; | ||
} | ||
|
||
@Override | ||
protected boolean isEnabled() { | ||
return this.properties.getInitializer().isEnabled(); | ||
} | ||
|
||
@Override | ||
protected String getSchemaLocation() { | ||
return this.properties.getSchema(); | ||
} | ||
|
||
@Override | ||
protected String getDatabaseName() { | ||
String databaseName = super.getDatabaseName(); | ||
if ("db2".equals(databaseName)) { | ||
return "db2_v95"; | ||
} | ||
if ("mysql".equals(databaseName)) { | ||
return "mysql_innodb"; | ||
} | ||
if ("postgresql".equals(databaseName)) { | ||
return "postgres"; | ||
} | ||
if ("sqlserver".equals(databaseName)) { | ||
return "sqlServer"; | ||
} | ||
return databaseName; | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
...nfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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 java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* Configuration properties for the Quartz Scheduler integration. | ||
* | ||
* @author Vedran Pavic | ||
* @since 2.0.0 | ||
*/ | ||
@ConfigurationProperties("spring.quartz") | ||
public class QuartzProperties { | ||
|
||
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/quartz/impl/" | ||
+ "jdbcjobstore/tables_@@platform@@.sql"; | ||
|
||
private final Initializer initializer = new Initializer(); | ||
|
||
/** | ||
* Additional Quartz Scheduler properties. | ||
*/ | ||
private Map<String, String> properties = new HashMap<>(); | ||
|
||
/** | ||
* Path to the SQL file to use to initialize the database schema. | ||
*/ | ||
private String schema = DEFAULT_SCHEMA_LOCATION; | ||
|
||
public Initializer getInitializer() { | ||
return this.initializer; | ||
} | ||
|
||
public Map<String, String> getProperties() { | ||
return this.properties; | ||
} | ||
|
||
public void setProperties(Map<String, String> properties) { | ||
this.properties = properties; | ||
} | ||
|
||
public String getSchema() { | ||
return this.schema; | ||
} | ||
|
||
public void setSchema(String schema) { | ||
this.schema = schema; | ||
} | ||
|
||
public class Initializer { | ||
|
||
/** | ||
* Create the required Quartz Scheduler tables on startup if necessary. Enabled | ||
* automatically if the schema is configured. | ||
*/ | ||
private boolean enabled = true; | ||
|
||
public boolean isEnabled() { | ||
return this.enabled && QuartzProperties.this.getSchema() != null; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add initializeBean after autowireBean to call postcontructs of injected beans?
http://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring/15211030#comment59382309_15211030
Maybe it is good to have a unit test for this? What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, will look into it.
If you refer to job auto-wiring support, it's already tested, take a look at
QuartzAutoConfigurationTests#withConfiguredJobAndTrigger
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's a known limitation, but due to the fact that it uses
Object jobInstance = super.createJobInstance(bundle);
which is implemented with:bundle.getJobDetail().getJobClass().newInstance();
fromAdaptableJobFactory
constructor injection in Job beans is not possible.To make it work, could we simply do:
Object job = this.beanFactory.createBean(bundle.getJobDetail().getJobClass());
and then apply properties population fromSpringBeanJobFactory
? It seems to work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do this we basically drop the standard
SpringBeanJobFactory
behavior since we don't call thesuper.createJobInstance(bundle)
anymore, right?BTW I wonder if
JobFactory
implementation is Boot's business after all. If there are any improvements in that department I guess more natural place would be Spring Core, together with the rest of Quartz Scheduler support. Thoughts on this @snicoll?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constructor injection is not something that is supported currently so that seems quite unrelated to this PR. If you want to support construtor injection, then please raise an issue in the Spring Framework issue tracker. The whole quartz support works currently with properties injection and I don't think that's something we could/should fix in Spring Boot.
@vpavic that factory is indeed a bit more opinionated. It's fine to put it here for the time being since it's hidden. We can revisit this if we integrate the feature in the framework. I'll discuss that with @jhoeller