Quartz Scheduler with Spring


You have already seen how to build a scheduler using quartz. With already providing
support to any possible tech feature in today's world, it's no surprise that Spring has
support for quartz. We will see here how you can build quartz application using
Spring.

Again for this project also we are going to take help from maven.

If you don't know how to create a simple maven project , check this link - Create a Simple Maven Project

Let's go step by step.

1. Create a simple maven project.

2. We are using Spring 3.0 version here, agree it is bit old, but I guess no significant change has been done ( if any) for quartz support.

Enter this dependencies in pom.xml :


  
  
   org.springframework
   spring-core
   3.0.4.RELEASE
  

  
  
   org.springframework
   spring-context-support
   3.0.4.RELEASE
  

  
  
   org.springframework
   spring-tx
   3.0.4.RELEASE
  

  
  
   org.quartz-scheduler
   quartz
   1.8.5
  

 
3. Now under your 'src' folder make two sub-folder, namely : 'com/java' and 'resource'.










    a. In 'com/java' folder we need to create two class:

       i. HelloWorldJob.java and it goes like this:
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class HelloWorldJob extends QuartzJobBean  {

 @Override
 protected void executeInternal(JobExecutionContext context)
   throws JobExecutionException {
  System.out.println("hello world!!!!!");

 }

}
This class extends an abstract class QuartzJobBean and override one method namely executeInternal. This class is responsible for whatever job you are intending to do using quartz. In this case we are simply printing 'hello world!!!!!” in the console. No big deal. You can implement the method in anyway you want. Let's call this class now:

 ii. Create another class in the same package (com.java): LaunchJob.java and it goes like this:
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class LaunchJob {

 /**
  * @param args
  */
 public static void main(String[] args) {
  new FileSystemXmlApplicationContext("src/resource/quartz-batchjobs.xml");

 }

}
This is a simple class with main method which will load the spring quartz xml file. Nothing special here.

4. By this time you obviously have figured out that there will be an xml file which will contain all the details for the job and the trigger. In the 'resource' folder under 'src', create an xml namely quartz-batchjobs.xml and the file goes like this:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 <bean class="org.springframework.scheduling.quartz.JobDetailBean" id="helloWorldJob">
 <property name="jobClass" value="com.java.HelloWorldJob">
 </property></bean>
 <bean class="org.springframework.scheduling.quartz.CronTriggerBean" id="helloWorldJobTrigger">
  <property name="jobDetail" ref="helloWorldJob">
  <property name="cronExpression" value="0 0/1 * 1/1 * ? *">
 </property></property></bean>
 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" id="schedulerFactoryBean">

  <property name="jobDetails">
   <list>
    <ref bean="helloWorldJob">
   </ref></list>
  </property>
  <property name="triggers">
   <list>
    <ref bean="helloWorldJobTrigger">
   </ref></list>
  </property>
 </bean>
</beans>

Explanation: Three bean has been defined here:
i.
This bean is an instance of JobDetailBean of spring framework. It can be feeded with many argument, we are passing only one with name: “jobClass” and the value should be your jobclass name .
ii.

This bean is an instance of CronTriggerBean of spring framework. We have passed two argument here :


For “jobDetail” we need to pass the reference of our job class For “cronExpression” we need to pass a cron expression which you can simply get from http://cronmaker.com. In this case we have defined the expression so our job get executed in every one minute.

 iii. The third bean is where first two bean is coming to real use



This property expects the reference of our job class

This property expects the reference of our trigger class.

You can omit the <list> </list> . With <list>  you can provide more than one reference for both job details and trigger, which is not needed in this case. 

 5> No other set up is required. Just right click on LaunchJob.java and select 'Run as Java Application'. In the console window you will get the output as 

hello world !!!! 
hello world !!!! 
hello world !!!! 
hello world !!!! 
…........................... 
every 1 minute it will print one line. 

Explanation: In LaunchJob.java we have written one line in the main method that sets the Spring container into action. 

new FileSystemXmlApplicationContext("src/resource/quartz-batchjobs.xml"); 

 The Spring container will load the quartz-batchjob.xml and start loading the beans. It will check the JobDetailBean , then the CronTriggerBean and call the executeInternal method of HelloWorldJob.java which you have defined as the Job Class. 
 So you can see, it is highly customizable, both the Job Class and Trigger can be changed easily and of course you can design multiple Job Class and Trigger in the xml file. 

 For any question or query please let me know.


Happy Learning !!

1 comment :

  1. Kiến thức của Bạn rất hay, thank chị đã chia sẻ.
    Xem tại website : Thiềm thừ

    ReplyDelete