Skip to content

Instantly share code, notes, and snippets.

@AdrianAtHumboldt
Created May 31, 2016 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdrianAtHumboldt/0885a39be05ceea8501b6661bbe8335d to your computer and use it in GitHub Desktop.
Save AdrianAtHumboldt/0885a39be05ceea8501b6661bbe8335d to your computer and use it in GitHub Desktop.
Updated Example of Custom Annotations for Quartz Jobs
package uk.co.humboldt.Application.Services;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
/**
* Based on http://sivalabs.in/2011/10/spring-and-quartz-integration-using-custom-annotation/
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Scope("prototype")
public @interface QuartzJob {
String name();
String group() default "DEFAULT_GROUP";
String cronExp();
}
package uk.co.humboldt.Application.Services;
import org.quartz.Job;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
/**
* Job factory to use spring beans as job objects
* Based on http://sivalabs.in/2011/10/spring-and-quartz-integration-using-custom-annotation/
*/
public class QuartzJobFactory extends SpringBeanJobFactory {
@Autowired
private ApplicationContext ctx;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Job job = ctx.getBean(bundle.getJobDetail().getJobClass());
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
bw.setPropertyValues(pvs, true);
return job;
}
}
package uk.co.humboldt.Application.Services;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import javax.annotation.PostConstruct;
import java.util.Set;
/**
* Scan for job implementation classes and schedule them.
* Based on http://sivalabs.in/2011/10/spring-and-quartz-integration-using-custom-annotation/
*/
public class QuartzJobScanner
{
@Autowired
private Scheduler scheduler;
private static final Logger log = LoggerFactory.getLogger(QuartzJobScanner.class);
private final String scanPackage;
public QuartzJobScanner(String scanPackage) {
this.scanPackage = scanPackage;
}
@PostConstruct
public void scheduleJobs() throws Exception
{
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
// Filter to include only classes that have a particular annotation.
provider.addIncludeFilter(new AnnotationTypeFilter(QuartzJob.class));
// Find classes in the given package (or subpackages)
Set<BeanDefinition> beans = provider.findCandidateComponents(scanPackage);
for (BeanDefinition bd: beans)
scheduleJob(bd);
}
private void scheduleJob(BeanDefinition bd) throws Exception
{
Class<?> beanClass = Class.forName(bd.getBeanClassName());
QuartzJob quartzJob = beanClass.getAnnotation(QuartzJob.class);
// Sanity check
if(Job.class.isAssignableFrom(beanClass) && quartzJob != null)
{
@SuppressWarnings("unchecked") Class<? extends Job> jobClass = (Class<? extends Job>)(beanClass);
log.info("Scheduling quartz job: " + quartzJob.name());
JobDetail job = JobBuilder.newJob(jobClass)
.withIdentity(quartzJob.name(), quartzJob.group())
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule(quartzJob.cronExp()))
.withIdentity(quartzJob.name() + "_trigger", quartzJob.group())
.forJob(job)
.build();
scheduler.scheduleJob(job, trigger);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment