Google

Friday, February 06, 2009

Quartz scheduling with Spring

I was more than impressed how easy it was to implement a simple scheduling process in my J2EE application. I have a requirement that at defined periods in the week an email will be sent to a mailbox from my application. My application uses Spring which has built in support for Quartz and so all I had to do was this:


 

Add the following lines to my Ivy configuration to get the necessary binaries in the Ant build (it's not too dissimilar if you are using Maven for your POM):


 

<dependency org="opensymphony" name="quartz" rev="1.6.0"/>

<dependency org="commons-collections" name="commons-collections" rev="3.2"/>


 

And in my Spring config all I added was:


 

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">

     <property name="triggers">    

     <ref bean="simpleTrigger"/>    

     </property>    

    </bean>

    

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">    

     <property name="jobDetail" ref="jobDetail"/>

    <!-- 10 seconds -->

     <property name="startDelay" value="10000"/>    

     <property name="repeatInterval" value="10000"/>

    </bean>


 

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

     <property name="targetObject" ref="voyagerPaymentSpreadsheetImpl"/>

     <property name="targetMethod" value="emailPaymentSpreadsheet"/>

     <property name="concurrent" value="true"/>

    </bean>


 

Just to add some notes to this, the ShedulerFactoryBean
MUST NOT HAVE lazy initialisation otherwise Spring will wait for it to be called before it instantiates it, and therefore not ideal for a background scheduler:


 

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">


 

The SimpleTrigger is one I'm using for testing and runs the scheduler every 10 seconds, but in production I'll move this to be a CRON schedule, similar to this:


 

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail" ref="exampleJob"/>

    <!-- run every morning at 6 AM -->

<property name="cronExpression" value="0 0 6 * * ?"/>

</bean>


 

Finally the job detail is quite self explanatory but basically says when the scheduler kicks in call the method, targetMethod, on the object, targetObject.


 

It's as easy as that… just plug and play… Of course I got most of this from the Spring documentation and by a bit of fiddling myself to get it working, but it works a treat!

3 Comments:

Blogger Chris Brind said...

Looks awesome, if you have an XML fetish! :)

Are you a software developer or an XML author? ;)

8:45 PM  
Blogger Andy Latham said...

LOL - Dude, I HAVE got an XML fetish... I believe in the KISS principle, if I can keep my code down to a minimum to do the job and hook it together with a few lines of XML config and save on all the hard to test glue code then I defo will :-)

2:43 PM  
Blogger Chris Brind said...

I missed your response.

Hmm, the thing about coding is that there's two levels of testing and you get one for free. Compilation can be a great tool for testing your code, if you design your code correctly. ;)

However, this entry is a very good example of how lazy developers can quickly add functionality to their applications and there's nothing wrong with being lazy!!!

11:28 AM  

Post a Comment

<< Home