JDK Timer Support
You also have the option of using JDK’s Timer support to schedule jobs. The various steps in doing this integration is as summarized below.
1. Configure scheduler job in Spring configuration file.
Similar to quartz configuration, you can do this in two ways as shown below:
a. Using MethodInvokingTimerTaskFactoryBean
Simplest way to configure a scheduler in a Spring application. We will be using the same class that we used in Quartz, for explaining this as well so that you can relate things better. Listing 20-28 shows configuring this factory bean in the Spring configuration file as bean.
Listing 20-28. Configuring MethodInvokingTimerTaskFactoryBean as spring bean
1 2 3 4 5 |
<bean id="scheduledTask" class="com.mybook.sample.schedule.ScheduledTask" /> <bean id="runScheduledJob" class="org.springframework.scheduling.tmer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject" ref="scheduledTask" /> <property name="targetMethod" value="scheduledMethod" /> </bean> |
b. Using TimerTask
First you will have to create a class extending java.util.TimerTask and implement the method “run” in which the method which needs scheduling to be called as shown in the Listing 20-29 below.
Listing 20-29. Creating class extending TimerTask
1 2 3 4 5 6 7 8 9 10 11 |
package com.mybook.sample.schedule; import java.util.TimerTask; public class RunScheduledJob extends TimerTask { private ScheduledTask scheduledTask; public void setScheduledTask(ScheduledTask scheduledTask) { this.scheduledTask = scheduledTask; } public void run(){ scheduledTask.scheduledMethod (); } } |
The configuration of timer task bean is as shown in Listing 20-30 below.
Listing 20-30. Configuring TimerTask class in the Spring configuration file
1 2 3 4 5 6 7 8 9 10 |
<bean id="scheduledTask" class="com.mybook.sample.schedule.ScheduledTask" /> <bean id="runScheduledJob" class="com.mybook.sample.schedule.RunScheduledJob"> <property name=”scheduledTask” ref=”scheduledTask”/> </bean> <bean name="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="delay" value="10000" /> <!-- run every 50 seconds --> <property name="period" value="50000" /> <property name="timerTask" ref="runScheduledJob" /> </bean> |
2. Configure the timer factory.
You will now have to configure the timer factory as shown in the Listing 20-31 below.
Listing 20-31. Configuring time factory in Spring configuration file
1 2 3 4 5 6 7 |
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTimerTask" /> </list> </property> </bean> |
3. Test Class
Test class just boot straps the Spring configuration file as shown in Listing 20-32 below.
Listing 20-32. 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"); } } |
Summary
Spring’s asynchronous support abstracts away most of the complex threading logic for you and still provides full access to the implementation behind. It also supports the new asynchronous API in Java EE 6 without any configuration changes.
The asynchronous abstraction that Spring provides is very handy and easy to use. It also hides much of the details of auto-proxying asynchronous calls from the developer. The downside is that it hides the complexity, and when issues arise, might be difficult to track down the root cause.
We started off this chapter with some very important concepts and then moved on to the support which Java provides to achieve these functionalities in your application. Then we took a deep dive into Spring’s abstraction of these services, and finally, we ended the chapter showing a detailed example of integration of Spring with third-party scheduler service Quartz.
After reading this chapter you should have a clear idea of task scheduling support provided by Java language API’s and the abstraction which Spring provides. You should also be able to configure and use Quartz in your Spring application as well have the capability of using JDK’s timer support in your application.
Page Visitors: 25293
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