Spring Book – Chapter 19 – Tasks and Scheduling

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

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

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

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

3. Test Class

Test class just boot straps the Spring configuration file as shown in Listing 20-32 below.

Listing 20-32. Test class

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: 24516

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *