Enterprise Java

Spring & Quartz Integration with Custom Annotation

We know Spring has support for integration with the Quartz framework. But as of now Spring supports only static XML declarative approach.

If you want to see how to integrate Spring with Quartz you can refer to the Spring + Quartz + JavaMail Integration Tutorial.

As part of my pet project requirements I got to schedule the Jobs dynamically and I thought of the following 2 options:

1. Using Annotations for providing Job Metadata
2. Loading the Job Metadata from Database

For now I thought of going ahead with the Annotation based approach and I want to integrate it with Spring as well. Here is how I did it.

1. Create a Custom Annotation QuartzJob

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.sivalabs.springsamples.jobscheduler;
 
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
import org.springframework.stereotype.Component;
 
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Scope("prototype")
public @interface QuartzJob
{
  
 String name();
 String group() default "DEFAULT_GROUP";
 String cronExp();
}

2. Create an ApplicationListener to scan for all the Job implementation classes and schedule them using Quartz scheduler.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.sivalabs.springsamples.jobscheduler;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.JobDetailBean;
 
public class QuartJobSchedulingListener
    implements ApplicationListener<ContextRefreshedEvent>
{
 @Autowired
 private Scheduler scheduler;
  
 @Override
 public void onApplicationEvent(ContextRefreshedEvent event)
 {
  try
  {
    ApplicationContext applicationContext = event.getApplicationContext();
    List<CronTriggerBean> cronTriggerBeans = this.loadCronTriggerBeans(applicationContext);
    this.scheduleJobs(cronTriggerBeans);
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
 }
  
 private List<CronTriggerBean> loadCronTriggerBeans(ApplicationContext applicationContext)
 {
   Map<String, Object> quartzJobBeans =
    applicationContext.getBeansWithAnnotation(QuartzJob.class);
   
   Set<String> beanNames = quartzJobBeans.keySet();
   
   List<CronTriggerBean> cronTriggerBeans = new ArrayList<CronTriggerBean>();
   
   for (String beanName : beanNames)
   {
     CronTriggerBean cronTriggerBean = null;
     Object object = quartzJobBeans.get(beanName);
     System.out.println(object);
     try
     {
      cronTriggerBean = this.buildCronTriggerBean(object);
     }
     catch (Exception e)
     {
      e.printStackTrace();
     }
    
     if(cronTriggerBean != null)
     {
      cronTriggerBeans.add(cronTriggerBean);
     }
   }
   return cronTriggerBeans;
 }
  
 public CronTriggerBean buildCronTriggerBean(Object job) throws Exception
 {
   CronTriggerBean cronTriggerBean = null;
   QuartzJob quartzJobAnnotation =
     AnnotationUtils.findAnnotation(job.getClass(), QuartzJob.class);
      
   if(Job.class.isAssignableFrom(job.getClass()))
   {
     System.out.println("It is a Quartz Job");
     cronTriggerBean = new CronTriggerBean();
     cronTriggerBean.setCronExpression(quartzJobAnnotation.cronExp());   
     cronTriggerBean.setName(quartzJobAnnotation.name()+"_trigger");
     //cronTriggerBean.setGroup(quartzJobAnnotation.group());
     JobDetailBean jobDetail = new JobDetailBean();
     jobDetail.setName(quartzJobAnnotation.name());
     //jobDetail.setGroup(quartzJobAnnotation.group());
     jobDetail.setJobClass(job.getClass());
     cronTriggerBean.setJobDetail(jobDetail);  
   }
   else
   {
    throw new RuntimeException(job.getClass()+" doesn't implemented "+Job.class);
   }
   return cronTriggerBean;
 }
  
 protected void scheduleJobs(List<CronTriggerBean> cronTriggerBeans)
 {
  for (CronTriggerBean cronTriggerBean : cronTriggerBeans)
  {
    JobDetail jobDetail = cronTriggerBean.getJobDetail();
    try
    {
     scheduler.scheduleJob(jobDetail, cronTriggerBean);
    }
    catch (SchedulerException e)
    {
     e.printStackTrace();
    }  
  }
 }
}

3. Create a customized JobFactory to use Spring beans as Job implementation objects.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.sivalabs.springsamples.jobscheduler;
 
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;
 
public class SpringQuartzJobFactory extends SpringBeanJobFactory
{
 @Autowired
 private ApplicationContext ctx;
 
 @Override
 protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception
 {
     @SuppressWarnings("unchecked")
  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;
 }
}

4. Create the Job implementation classes and Annotate them using @QuartzJob

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
package com.sivalabs.springsamples.jobscheduler;
 
import java.util.Date;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
 
@QuartzJob(name="HelloJob", cronExp="0/5 * * * * ?")
public class HelloJob extends QuartzJobBean
 @Override
 protected void executeInternal(JobExecutionContext context)
   throws JobExecutionException
 {
  System.out.println("Hello Job is running @ "+new Date());
  System.out.println(this.hashCode()); 
 }
}

5. Configure the SchedulerFactoryBean and QuartJobSchedulingListener in applicationContext.xml

01
02
03
04
05
06
07
08
09
10
11
12
<beans>
 <context:annotation-config></context:annotation-config>
 <context:component-scan base-package="com.sivalabs"></context:component-scan>
  
 <bean class="com.sivalabs.springsamples.jobscheduler.QuartJobSchedulingListener"></bean>
 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="jobFactory">
   <bean class="com.sivalabs.springsamples.jobscheduler.SpringQuartzJobFactory"></bean>
  </property>
 </bean>
  
</beans>

6. Use a Test Client to launch the context

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
package com.sivalabs.springsamples;
 
import org.quartz.Job;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.sivalabs.springsamples.jobscheduler.HowAreYouJob;
import com.sivalabs.springsamples.jobscheduler.InvalidJob;
 
public class TestClient
{
 public static void main(String[] args)
 {
  ApplicationContext context =
  new ClassPathXmlApplicationContext("applicationContext.xml");
  System.out.println(context); 
 }
 
}

Reference: Spring and Quartz Integration Using Custom Annotation from our JCG partner Siva at “My Experiments on Technology” Blog.

Related Articles :
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Siva Reddy

Katamreddy Siva Prasad is a Senior Software Engineer working in E-Commerce domain. His areas of interest include Object Oriented Design, SOLID Design principles, RESTful WebServices and OpenSource softwares including Spring, MyBatis and Jenkins.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Prasad
Prasad
9 years ago

Hello Siva Prasad ..!

Thanks for above post its really potential code .

in my company I have a requirement like
1.I need to write the thread scheduler using quartz&spring which will trigger my API for every 5 sec .

matan
matan
7 years ago

Thanks for the great post
Is this post still relevant ?

Back to top button