Spring + Quartz scheduler example
Spring provides many handy classes to support the Quartz, and decouple your class to Quartz API.
P.S You can check the Quartz without Spring support example here.
1. Scheduler Task
Create a normal Java class, this is the class you want to schedule.
package com.mkyong.common; public class RunMeTask { public void printMe() { System.out.println("Run Me ~"); } }
Bean configuration file.
<bean id="runMeTask" class="com.mkyong.common.RunMeTask" />
2. Scheduler Job + JobDetail
With Spring support, you can declare the Quartz job in two ways :
1. MethodInvokingJobDetailFactoryBean
This is the simplest and straightforward method, suitable for simple scheduler. You can specify a ‘MethodInvokingJobDetailFactoryBean‘ class to configure the class and method you want to schedule.
<bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="runMeTask" /> <property name="targetMethod" value="printMe" /> </bean>
2. JobDetailBean
The QuartzJobBean is more flexible and suitable for complex scheduler. You need to create a class extends the Spring’s QuartzJobBean, and define the method you want to schedule in executeInternal() method, and pass the scheduler task (RunMeTask) via setter method.
package com.mkyong.common; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class RunMeJob extends QuartzJobBean { private RunMeTask runMeTask; public void setRunMeTask(RunMeTask runMeTask) { this.runMeTask = runMeTask; } protected void executeInternal(JobExecutionContext context) throws JobExecutionException { runMeTask.printMe(); } }
Configure the target class (jobClass) and method (jobDataAsMap).
<bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.mkyong.common.RunMeJob" /> <property name="jobDataAsMap"> <map> <entry key="runMeTask" value-ref="runMeTask" /> </map> </property> </bean>
3. Trigger
Configure the Quartz trigger to define when will run your scheduler job. Two type of triggers are supported :
1. SimpleTrigger
It allows to set the start time, end time, repeat interval to run your job. In this simpleTrigger example, it will run the printMe() method in every 5 seconds with a 1 second delay for the first time of execution.
<!-- Simple Trigger --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="repeatInterval" value="5000" /> <property name="startDelay" value="1000" /> </bean>
2. CronTrigger
It allows Unix cron expression to specify the dates and times to run your job. In this cronTrigger example, it will run the printMe() method in every 5 seconds.
<!-- Cron Trigger --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="cronExpression" value="0/5 * * * * ?" /> </bean>
The Unix cron expression is highly flexible and powerful, you can learn and see many advance cron expression examples in following website.
1. http://en.wikipedia.org/wiki/CRON_expression
2. http://www.quartz-scheduler.org/docs/examples/Example3.html
4 Scheduler
Create a Scheduler factory bean to integrate your job detail and trigger together.
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="runMeJob" /> </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean>
Full Spring’s bean configuration file
Spring-Quartz.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="runMeTask" class="com.mkyong.common.RunMeTask" /> <bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.mkyong.common.RunMeJob" /> <property name="jobDataAsMap"> <map> <entry key="runMeTask" value-ref="runMeTask" /> </map> </property> </bean> <!-- <bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="runMeTask" /> <property name="targetMethod" value="printMe" /> </bean> --> <!-- Simple Trigger --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="repeatInterval" value="5000" /> <property name="startDelay" value="1000" /> </bean> <!-- Cron Trigger --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="cronExpression" value="0/5 * * * * ?" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="runMeJob" /> </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean> </beans>
Run it
package com.mkyong.common; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) throws Exception { new ClassPathXmlApplicationContext("Spring-Quartz.xml"); } }
Done.