OpenSymphony Quartz Scheduler in Spring

Spring offers a very seamless integration with Quartz to create some scheduled tasks. Below i will explain the steps in which you can do that.

To get started what you need is a spring.jar downloaded along with a download of Quartz jar files.

Spring simply gives you extension of all the core classes provided by Quartz which includes:
1. org.springframework.scheduling.quartz.JobDetailBean - which extends QuartzJobBean
2. org.springframework.scheduling.quartz.SimpleTriggerBean - which extends SimpleTrigger
3. org.springframework.scheduling.quartz.SchedulerFactoryBean - which implements the main scheduling functionality.

So by using these three classes you can write scheduled jobs very quickly. All you need to do is defined three beans in your spring - config file. The definition is as follows.

Declare the job:


<bean name="sendmailsjob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.thoughtclicks.SendEmailsJob" />
<property name="jobDataAsMap">
<map>
<entry key="to" value="rahul.juneja@thoughtclicks.com" />
</map>
</property>
</bean>


Now add the job class as follows.

public class SendEmailsJob extends QuartzJobBean {
@Override
protected void executeInternal (JobExecutionContext ctx) throws JobExecutionException {
String touser = (String) ctx.getJobDetail().getJobDataMap().get("to");
sendmail(to);//this will send the mail to the user defined in the to address.
}
}


You have a choice of fetching the properties from the context i have done in the previous code or you can also add the properties in the bean for the same and provide getter and setters and Spring will do the injection for you and provide the populated properties.

now you need to define the trigger using the job you created above.


<bean id="emailTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="sendmailsjob" />
<property name="startDelay" value="0" />
<property name="repeatInterval" value="10000" />
</bean>


Now finally you need to define the scheduler for scheduling this very trigger, which can be done as follows.


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="emailTrigger" />
</list>
</property>
</bean>


After the steps defined above once you start the application including the spring config the job you just created is started automatically and will run in every 10 seconds as defined in the trigger with property "repeat interval"

You can also use your existing methods to be called by the scheduler rather then creating the whole new scheduler bean and the only change you need for that is creating the job as follows.


<bean name="sendmailsjob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="existingbeanallreadydefined" />
<property name="targetMethod" value="methodsendMail" />
</bean>


If you need any further code or tips. you can drop me an email at rahul.juneja@thoughtclicks.com