Integration with Third-party Scheduling Service
Following the philosophy of Spring Framework, it does allow integrating other third party software’s with ease. In this section we will see the integration of Spring application with a popular scheduling service, Quartz, in full detail.
Quartz
Quartz is a full-featured, open source job scheduling service that can be integrated with, or used alongside virtually any Java EE or Java SE application – from the smallest stand-alone application to the largest E-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that are programmed to fulfill the requirements of your application. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.
– Quartz Documentation
Integration with Spring
Integrating Quartz to Spring is a piece of cake as both Quartz and Spring has appropriate extensions to plug onto. To integrate your Spring application with Quartz, you will have to do the following steps as summarized below.
1. Project Dependency
Based on the build tool that you are using, put the Quartz dependency jars onto your Spring project.
2. Create Scheduler Class
This is the class which has the method which you would like to schedule using the quartz scheduler. Listing 20-20 shows the class and the single method it contains, which will be scheduled using Quartz scheduler.
Listing 20-20. Scheduled Class and its Method
1 2 3 4 5 6 |
package com.mybook.sample.schedule; public class ScheduledTask { public void scheduledMethod() { System.out.println("Do this job…."); } } |
3. Declare the Quartz Scheduler Job in the Spring configuration file
Declare the Quartz Scheduler Job in the Spring configuration file applicationContext.xml. The configuration of scheduler job can be done by two ways as given below:
a. Using MethodInvokingJobDetailFactoryBean
This is the simplest way to configure a scheduler in a Spring application. Using this way dependency of your class with the Spring API can be reduced as the configuration is in the configuration file. Listing 20-21 shows configuring this factory bean in the Spring configuration file as bean.
Listing 20-21. Configuring MethodInvokingJobDetailFactoryBean as spring bean
1 2 3 4 5 |
<bean id="scheduledTask" class="com.mybook.sample.schedule.ScheduledTask" /> <bean id="runScheduledJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="scheduledTask" /> <property name="targetMethod" value="scheduledMethod" /> </bean> |
b. Using JobDetailBean
Using this method introduces dependency of your application to the Spring API’s for scheduling. First of all you will have to create a class extending org.springframework.scheduling.quartz.QuartzJobBean and implement the method “executeInternal” in which the method which needs scheduling to be called as shown in the Listing 20-22 below.
Listing 20-22. Creating JobDetailBean by extending QuartzJobBean
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.mybook.sample.schedule; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class RunScheduledJob extends QuartzJobBean { private ScheduledTask scheduledTask; public void setScheduledTask(ScheduledTask scheduledTask) { this.scheduledTask = scheduledTask; } protected void executeInternal(JobExecutionContext context) throws JobExecutionException { scheduledTask.scheduledMethod (); } } |
If you do this way of configuring the scheduler job, you will have to configure this scheduler job as shown in Listing 20-23 below.
Listing 20-23. Configuring JobDetailBean class
1 2 3 4 5 6 7 8 9 |
<bean id="scheduledTask" class="com.mybook.sample.schedule.ScheduledTask" /> <bean name="runScheduledJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.mybook.sample.schedule.RunScheduledJob" /> <property name="jobDataAsMap"> <map> <entry key="scheduledTask" value-ref="scheduledTask" /> </map> </property> </bean> |
4. Configure trigger in Spring configuration file.
Configure Quartz trigger which will decide when to run the job which is configured already in previous steps. Two different types of triggers can be created and configured. They are:
a. Simple Trigger
Configuration of simple trigger is as shown in Listing 20-24 below.
Listing 20-24. Configuring simple trigger in Spring configuration
1 2 3 4 5 |
<bean id="simpleTriggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="runScheduledJob" /> <property name="repeatInterval" value="10000" /> <property name="startDelay" value="2000" /> </bean> |
b. Cron Trigger
Configuration of simple trigger is as shown in Listing 20-25 below.
Listing 20-25. Configuring simple trigger in Spring configuration
1 2 3 4 |
<bean id="cronTriggerBean"> <property name="jobDetail" ref="runScheduledJob" /> <property name="cronExpression" value="0/5 * * * * ?" /> </bean> |
5. Configure the scheduler factory.
Link the job and the trigger using the scheduler factory as shown in Listing 20-26 below.
Listing 20-26. Configure scheduler factory in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<bean> <property name="jobDetails"> <list> <ref bean="runScheduledJob" /> </list> </property> <property name="triggers"> <list> <!-- Use simple or cron trigger bean --> <ref bean="simpleTriggerBean" /> </list> </property> </bean> |
6. Test class
Test class for seeing the scheduling of job using Quartz in action is as shown in Listing 20-27 below.
Listing 20-27. Test class
1 2 3 4 5 6 7 |
package com.mybook.sample.schedule; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestClass { public static void main( String[] args ) { new ClassPathXmlApplicationContext("applicationContext.xml"); } } |
Very simple right, actually nothing needs to be done after bootstrapping the Spring configuration file because the job will be run according to the schedule that we have configured using trigger.
Page Visitors: 25297
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023