Spring integration with other technology or frameworks
Spring and Quartz Timer
I will not talk more about spring or quartz. I am assuming that you must know spring and quartz. I will explain you how to integrate quartz with Spring framwork.
First i will show you how to create timer using quartz.
Quartz offers five main classes/Interfaces for scheduling:
- The Job interface
- The JobDetail class
- The Trigger abstract class
- The Scheduler interface
- The SchedulerFactory interface
The Job interface represents a job. A job offers what task or work you want to perform. Only one execute() method,
which will be called by Quartz when a Job has to be executed. You can retrieve info like job name, trigger and many more info from JobExecutionContext that
is passed to the execute() method. Below example shows sample job class.
public class MyJob implements Job { public void execute (JobExecutionContext ctx) throws JobExecutionException { System.out.println("Welcome Chirag"); } }
>
The JobDetail class is for giving some information about a particular Job. Jobs will be started (or "fired") by triggers, which are represented by the Trigger class which are SimpleTrigger and CronTrigger. A SimpleTrigger is a basic timer, where we can declare a start time, an end time, no of times repeated and after what time it should be repeated. Second trigger is CronTrigger in which we can declare complex timer declaration, using the "cron" like Unix cron job notation. E.g. "fire the job at 1:15am on every Tuesday in january". Last one is the SchedulerFactory which is used to get a instance of Scheduler, using which we can register jobs and triggers.
public class MyJobMain { public static void main (String[] args) { try { SchedulerFactory schedFactory = new StdSchedulerFactory(); Scheduler mySched = schedFactory.getScheduler(); mySched.start(); JobDetail jobDetail = new JobDetail("MyJob", null, MyJob.class); // Fires every 50 seconds Trigger trigger = TriggerUtils.makeSecondlyTrigger(50); trigger.setName("myTrigger"); mySched.scheduleJob(jobDetail, trigger); } catch (SchedulerException ex) { ex.printStackTrace(); } } } Output: After executing above program Welcome Chirag is being printed out every 50 seconds. Now We will look quartz integration with Spring step by step 1) Classes provided by Spring for Quartz
- The QuartzJobBean abstract class
- The JobDetailBean class
- The SimpleTriggerBean class
- The CronTriggerBean class
- The SchedulerFactoryBean class
- The MethodInvokingJobDetailFactoryBean class
All above classes are more or less same as quartz API except MethodInvokingJobDetailFactoryBean whch has one advantage that we will see in last section. 2) How to declare jobs in spring context using JobDetailBean The JobDetailBean is used to specify jobs that we want to execute. We can set the name of the job class. The Spring Framework provides a JobDetailBean that makes the JobDetail more of an actual JavaBean with sensible defaults. . Here is the declaration :
<bean name="myJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.test.MyJob" /> <property name="jobDataAsMap"> <map> <entry key="tips" value="Welcome Chirag" /> </map> </property> </bean>
And the declaration job class which is extending QuartzJobBean class and overriding executeInternal method. We can customize this job class and method using MethodInvokingJobDetailFactoryBean that we will look in last section :
public class MyJob extends QuartzJobBean { private String message; public void setMessage (String message) { this.message = message; } @Override protected void executeInternal (JobExecutionContext ctx) throws JobExecutionException { String tips = (String) ctx.getJobDetail().getJobDataMap().get("tips"); System.out.println("Message comes from context:" + tips); } }
3) How to declare triggers in spring context using SimpleTriggerBean or CronTriggerBean
Now We have created job details and jobs. Now time to schedule the jobs themselves. This is done using triggers and a SchedulerFactoryBean like we did in simple quartz without using spring framework.
Triggers need to be scheduled. Spring offers a SchedulerFactoryBean that exposes triggers to be set as properties. SchedulerFactoryBean job is to schedule the actual jobs with using triggers.
Find below examples of simple and cron trigger:
<bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="myJob" /> <property name="startDelay" value="5" /> <!-- This will run after every 5 seconds --> <property name="repeatInterval" value="5000" /> </bean> <bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="myJob" /> <!-- It will run onevery morning at 10 AM --> <property name="cronExpression" value="0 0 10 * * ?" /> </bean>
Now we have declared two triggers, one running every 5 seconds with a starting delay of 5 seconds and second cron trigger running on every morning at 10 AM.
4) Schedule schedular using above job and trigger defined in Spring context Now we have declared job and associated trigger, its time to register the trigger. We will declare Spring's SchedulerFactoryBean the following way :
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="mySimpleTrigger"/> </list> </property> </bean>
Now Everything has been set up, all we need now is to load the context. The scheduler will be started automatically on initialization. I declared all the above beans in a file called"spring-quartz.xml".Now if you want to use crontrigger then just change the reference bean name in trigger list. you can define list of triggers but job name should be unique.
public class MyJobMain { public static void main (String[] args) throws SchedulerException { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-quartz.xml"); } }
Executing above MyJobMain program will start the scheduler automatically. As per our declaration The MyJob will be fired after 5 seconds of delay and repeated after every 5 seconds.
5) Use spring's MethodInvokingJobDetailFactoryBean
Many times we want to invoke a method on a specific class object. For that spring provides MethodInvokingJobDetailFactoryBean :
<bean id="myCustomJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myCustomJobTest" /> <property name="targetMethod" value="testIt" /> </bean>
The above example will result in the testIt method being called on the myCustomJobtest See below code for example
public class MyCustomJobtest { // properties public void testIt() { // do your job work } } <bean id="myCustomJobtest" class="com.test.MyCustomJobtest"/>
Using the MethodInvokingJobDetailFactoryBean, you don't need to create one-line jobs that just invoke a method, and you only need to create the actual business object and wire up the detail object.
One more property provided by spring in MethodInvokingJobDetailFactoryBean is non-concurrent, set the concurrent flag to false.
That's all for today. Will come back with other technology with Spring.