Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
/**
* 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 {
final Object job = super.createJobInstance(bundle);
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;
@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!
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;
}
}
@wbwiles

This comment has been minimized.

Copy link

commented Apr 16, 2015

Thank you!

@ajkret

This comment has been minimized.

Copy link

commented Jul 1, 2015

quartz.properties example?

@efi-mk

This comment has been minimized.

Copy link

commented Aug 23, 2015

What should we use in case we have post construct annotation ?

@panneerselvampst

This comment has been minimized.

Copy link

commented May 4, 2016

Thank you.. I am trying to create jobs and triggers dynamically like below . How i can inject new @bean(name = "testDynamciSJob") to schedulerfactorybean dynamically?

@bean(name = "testDynamciSJob")
public CronTriggerFactoryBean testDynamciSJob() throws ClassNotFoundException, ParseException {
System.out.println(
"TestDynamciJob constructor called]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]");
return createCronTrigger("com.jnj.scheduler.job.TestDynamciJob", "TestDynamciJob", "TestDynamciJob");
}

public JobDetailFactoryBean cronJobDetail(String classname,String jobName,String groupName) throws ClassNotFoundException {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setJobClass(Class.forName(classname));
jobDetailFactory.setGroup(groupName);
jobDetailFactory.setName(jobName);
jobDetailFactory.afterPropertiesSet();
jobDetailFactory.setDurability(true);
jobDetailFactory.afterPropertiesSet();
return jobDetailFactory;
}

public CronTriggerFactoryBean createCronTrigger(String classname,String name,String groupName) throws ClassNotFoundException, ParseException {
    CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
    cronTriggerFactoryBean.setJobDetail(cronJobDetail(classname,name,groupName).getObject());
    cronTriggerFactoryBean.setCronExpression(cronExpression);
    cronTriggerFactoryBean.setGroup(groupName);
    cronTriggerFactoryBean.setName(name);
    cronTriggerFactoryBean.afterPropertiesSet();
    // in case of misfire, ignore all missed triggers and continue :
    cronTriggerFactoryBean.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW);
    return cronTriggerFactoryBean;
}
@jijeshvu07

This comment has been minimized.

Copy link

commented Jul 8, 2016

How did you autowire ExampleRepository , beacuse when autowire a service bean it throws null pointer exception.

@lvillarino

This comment has been minimized.

Copy link

commented Aug 24, 2016

Please create a demo app.

@theflofly

This comment has been minimized.

Copy link

commented Aug 31, 2016

Nothing to say but thanks!

@adbourne

This comment has been minimized.

Copy link

commented Sep 20, 2016

Thank you!

@corberan

This comment has been minimized.

Copy link

commented Feb 27, 2017

Thank you very much!

@derrick-null

This comment has been minimized.

Copy link

commented May 13, 2017

Awesome ...Great solution

@shaowin16

This comment has been minimized.

Copy link

commented Aug 28, 2017

How did you import SchedulerFactoryBean?

@levkovych67

This comment has been minimized.

Copy link

commented Oct 5, 2017

Hello, I need to create jobs dynamically, how can i implement that ?

@bugmakesprogress

This comment has been minimized.

Copy link

commented Jul 31, 2018

Thans you so much! Solved inject problems

@Greetzkenny

This comment has been minimized.

Copy link

commented Oct 9, 2018

I came across a problem when trying your solution and I noticed that your ExampleService, who implements Job, has no public no-argument constructor. This is mandatory according to: http://www.quartz-scheduler.org/documentation/quartz-2.x/cookbook/DefineJobWithData.html

@thedevd

This comment has been minimized.

Copy link

commented Nov 22, 2018

Any one with full working example ?

@bkahlerventer

This comment has been minimized.

Copy link

commented Aug 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.