This post shows how you can integrate Spring with Quartz Scheduler which is a tool for scheduling jobs.
Using Quartz scheduler
Quartz uses Job, JobDetail and Trigger objects to realize scheduling of all kinds of jobs. Following are some of the main classes/interfaces in Quartz Scheduler API.
- Scheduler- This is the main interface of a Quartz Scheduler. A Scheduler maintains a registry of JobDetails and Triggers. Once registered, the Scheduler is responsible for executing jobs when their associated triggers fire (when their scheduled time arrives).
- Job- Job is an interface which is to be implemented by classes which represent a 'job' to be performed. Job interface has a single method execute(JobExecutionContext context) which is called by the Scheduler when a Trigger fires that is associated with the Job.
- JobDetail- Quartz does not store an actual instance of a Job class, but instead allows you to define an instance of one, through the use of a JobDetail interface.
- JobDataMap- This class holds state information for Job instances. JobDataMap instances are stored once when the Job is added to a scheduler.
- Trigger- Triggers are the 'mechanism' by which Jobs are scheduled. Triggers have a TriggerKey associated with them, which should uniquely identify them within a single Scheduler.
Spring integration with Quartz
Spring framework offers classes that simplify the usage of Quartz within Spring-based applications.
- QuartzJobBean class- This abstract class is a simple implementation of the Quartz Job interface. It also applies the passed-in job data map as bean property values.
- JobDetailFactoryBean- A Spring FactoryBean for creating a Quartz JobDetail instance, supporting bean-style usage for JobDetail configuration.
For example- Suppose you have a class EmailService for sending mails then it can be given as a property in a Job class than extends QuartzJobBean. The JobDetail instance can be configured using JobDetailFactoryBean where the job can be defined as a property along with JobDataMap.
- Refer Spring Email Scheduling Example Using Quartz Scheduler to see a email scheduling example in Spring using Quarts scheduler.
@Service public class MailReminderJob extends QuartzJobBean { private EmailService emailService; public void setEmailService(EmailService emailService) { this.emailService = emailService; } @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { .. ..
<bean name="emailReminderJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="org.netjs.service.MailReminderJob"/> <property name="jobDataAsMap"> <map> <entry key="emailService" value-ref="emailService"></entry> </map> </property> </bean>
Using MethodInvokingJobDetailFactoryBean
If you just need to invoke a method on a specific object you can use the MethodInvokingJobDetailFactoryBean to do that.
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="exampleBusinessObject"/> <property name="targetMethod" value="doIt"/> </bean>
The above configuration will result in the doIt method being called on the exampleBusinessObject bean.
Configuring triggers
By now you have job and job details, you also need to configure triggers for scheduling the jobs.
Spring integration with Quartz offers two Quartz FactoryBean implementations with convenient defaults-
- CronTriggerFactoryBean- Using this factory bean you can provide a cron expression.
- SimpleTriggerFactoryBean- Using this factory bean you can provide properties for job scheduling like start time, repeat interval, start delay.
An example configuration for SimpleTriggerFactoryBean and CronTriggerFactoryBean-
<bean id="simpleTrigger" class= "org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="jobDetail"/> <!-- 5 seconds --> <property name="startDelay" value="5000"/> <!-- repeat every 60 seconds --> <property name="repeatInterval" value="60000"/> </bean> <bean id="cronTrigger" class= "org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="exampleJob"/> <!-- run every morning at 8 AM --> <property name="cronExpression" value="0 0 8 * * ?"/> </bean>
Scheduling triggers
Triggers need to be scheduled. Spring offers a SchedulerFactoryBean that exposes triggers to be set as properties. SchedulerFactoryBean schedules the actual jobs with those triggers.
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger"/> <ref bean="simpleTrigger"/> </list> </property> </bean>
That's all for this topic Spring Integration With Quartz Scheduler. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Spring Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment