Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bugmakesprogress/8e200685613aab2e738fd46fa0850a14 to your computer and use it in GitHub Desktop.
Save bugmakesprogress/8e200685613aab2e738fd46fa0850a14 to your computer and use it in GitHub Desktop.
Quartz (2.1.6) java config with spring (3.2.1). Using a SpringBeanJobFactory to automatically autowire quartz classes.
package com.jelies.spring3tomcat7.config.quartz;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
/**
主要思路是通过新建一个AutowiringSpringBeanJobFactory 实现 quartz 的SpringBeanJobFactory(job生产工厂类)
通过实现ApplicationContextAware接口 set ApplicationContextAware(上下文)到该新建job工厂类。
然后通过super.createJobInstance(bundle) 创建 job, beanFactory来autowired job实例
* This JobFactory autowires automatically the created quartz bean with spring @Autowired dependencies.
*
* @author jelies (thanks to Brian Matthews: http://webcache.googleusercontent.com/search?q=cache:FH-N1i--sDgJ:blog.btmatthews.com/2011/09/24/inject-application-context-dependencies-in-quartz-job-beans/+&cd=7&hl=en&ct=clnk&gl=es)
*
*/
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements
ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
//Using SpringBeanJobFactory to create job
final Object job = super.createJobInstance(bundle);
//Using context to get AutowireCapableBeanFactory and using beanFactory to autowire job instance
beanFactory.autowireBean(job);
return job;
}
}
package com.jelies.spring3tomcat7.service.proceso;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jelies.spring3tomcat.model.entity.Example;
import com.jelies.spring3tomcat.repository.ExampleRepository;
import com.jelies.spring3tomcat.service.ExampleService;
/**
* Job instance are created by SpringBeanJobFactory instead of Quartz relative beanFactory.
* Under the circurstance, We can autowire quartz relative beans with spring annotation.
**/
@Service
@Transactional
public class ExampleService implements Job {
@Autowired
private ExampleRepository exampleRepository;
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
Example example = new Example();
example.setFoo("test");
exampleRepository.save(example);
}
}
package com.jelies.spring3tomcat7.config;
import java.io.IOException;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.quartz.Trigger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import com.jelies.spring3tomcat7.config.quartz.AutowiringSpringBeanJobFactory;
@Configuration
public class QuartzConfig {
private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName());
@Autowired
private DataSource dataSource;
@Autowired
private PlatformTransactionManager transactionManager;
@Autowired
private ApplicationContext applicationContext;
@PostConstruct
public void init() {
log.debug("QuartzConfig initialized.");
}
@Bean
public SchedulerFactoryBean quartzScheduler() {
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
quartzScheduler.setDataSource(dataSource);
quartzScheduler.setTransactionManager(transactionManager);
quartzScheduler.setOverwriteExistingJobs(true);
quartzScheduler.setSchedulerName("jelies-quartz-scheduler");
// custom job factory of spring with DI support for @Autowired! Important!
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
quartzScheduler.setJobFactory(jobFactory);
quartzScheduler.setQuartzProperties(quartzProperties());
Trigger[] triggers = { procesoMQTrigger().getObject() };
quartzScheduler.setTriggers(triggers);
return quartzScheduler;
}
@Bean
public JobDetailFactoryBean procesoMQJob() {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setJobClass(ExampleService.class);
jobDetailFactory.setGroup("spring3-quartz");
return jobDetailFactory;
}
@Bean
public CronTriggerFactoryBean procesoMQTrigger() {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(procesoMQJob().getObject());
cronTriggerFactoryBean.setCronExpression(0 * * * * ?);
cronTriggerFactoryBean.setGroup("spring3-quartz");
return cronTriggerFactoryBean;
}
@Bean
public Properties quartzProperties() {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
Properties properties = null;
try {
propertiesFactoryBean.afterPropertiesSet();
properties = propertiesFactoryBean.getObject();
} catch (IOException e) {
log.warn("Cannot load quartz.properties.");
}
return properties;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment