Tuesday, April 7, 2009

Spring Quartz

Let us now integrate Spring Quartz with the previous example. Let us check if there are any new match detail available every 1 hour. If we get new match detail; add it to the previously stored match details, create the new JSON feed and publish it.

Integrating Quartz Scheduler with Spring is very easy. You just have to add required beans into your bean definition file (applicationContext.xml). So, in our case it would look like –

< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >

< ! - -
This will call generateFeed method on feedGenerator bean defined below. To make jobs resulting from MethodInvokingJobDetailFactoryBean as non-concurrent, I have set the concurrent flag to false.
- - >

< bean id="jsonFeedJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
< property name="targetObject" ref="feedGenerator" />
< property name="targetMethod" value="generateFeed" />
< property name="concurrent" value="false" />
< /bean >

< ! - -
matchService bean is defined that has business logic to get the recent match detail. May be getting it from database or some other third party feed.
- - >
< bean id="matchService" class="test.SomeMatchService" / >

< ! - -
feedGenerator has the logic of generating JSON feed and has the method generateFeed(). SomeMatchService is injected into it
- - >
< bean id="feedGenerator" class="test.FeedGenerator"
p:matchService -ref="matchService"
/ >


< ! - -
Creates SimpleTriggerBean with start delay of 50 second and that repeats every 1 hour
- - >
< bean id="jsonFeedTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean" >
< property name="jobDetail" ref="jsonFeedJob" / >
< property name="startDelay" value="50000" / >
< property name="repeatInterval" value="3600000" / >
< / bean >

< ! - -
Finally adding the SimpleTriggerBean to SchedulerFactoryBean
- - >
< bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
< property name="triggers" >
< list >
< ref bean="jsonFeedTrigger" / >
< /list >
< /property >
< /bean >
< /beans >

Now add the following class to start your job

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JsonJobHandler {
public static void main (String [] args) {
String[] paths = { "file:applicationContext.xml" };
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
}
}

Instead of generating feed every 1 hour, if we want to generate feed every morning at 5:30 AM we can use CronTriggerBean instead of SimpleTriggerBean

<bean id="jsonFeedTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jsonFeedJob" />
<property name="cronExpression" value="0 30 5 * * ?" />
</bean>

Isn’t So Easy?

No comments: