Learn How to Schedule Tasks Using Spring

Schedule Tasks

Schedule Tasks

Task scheduling and asynchronous execution is an important feature of any framework or programming languages. Java has multiple implementation of thread pools, concurrent processing and execution. Spring framework is one of the most popular open source framework in the world of Java based enterprise/stand-alone application development. So, it has its own abstraction for concurrent task scheduling and execution.

In this article, we will explore how the execution and scheduling works in Spring framework. We will also check some code snippets to understand implementation in a better way.

What is TaskExecutor and TaskScheduler?
In this section, we will check the TaskExecutor and TaskScheduler one by one

TaskExecutor: –
Spring 2.0 has a new abstraction called TaskExecutor, it deals with executors. In Java 5, executors are basically thread pools. Spring’s abstraction layer simplified the use of executors. The TaskExecutor interface of Spring is identical to Java’s Executor interface. It is introduced to provide the facility of thread pools to different Spring components. Spring provides different implementations of TaskExecutor, like SimpleAsyncTaskExecutor, ConcurrentTaskExecutor, ThreadPoolTaskExecutor etc.

Let us check one simple implementation of TaskExecutor.

Listing 1: – Sample TaskExecutor implementation

In the following listing, we have configured a sample rule for the TaskExecutor. In this XML configuration, the core pool size is 6, maximum pool is 12 and the queue capacity is 30.

Listing 2: – Sample rule configuration

If we check the sample code carefully (Listing 1), we can understand that no thread is retrieved and executed from the pool manually. We are just adding the Runnable implementation in the queue, and the rest is taken care of by the internal rules of TaskExecutor. And, it manages all the threads internally. It is a great relief to the developers’ community, as they don’t need to worry about the thread management.

TaskScheduler: –
Spring 3.0 introduces TaskScheduler. It helps to schedule tasks at any point of time in the future. It also comes with features to check the status of tasks and even their cancellation. To implement TaskScheduler, we need to prepare a runnable task and select proper method to schedule it.

In the following listing, we can have a look at different methods offered by the interface.

Listing 3: – Showing methods in TaskScheduler interface

In the above methods, the simplest one is the one which contains the Runnable task and date. And, the method having Trigger is the complex one.

Let’s have a look at Triggers as it has some complex implementation.

The Trigger interface is a part of Spring 3.0. The basic idea is to control the execution time based on the past execution outcomes. The outcome of the previous execution is wrapped in a TriggerContext. TriggerContext is another important interface, which encapsulates all the relevant information.

Following code snippets shows the basic structure of Trigger and TriggerContext interface.

Listing 4: – Showing methods in Trigger interface

Listing 5: – Showing methods in TriggerContext interface

What are the ways to Schedule Tasks?
In this section, we will check how different type of scheduled tasks can be implemented. We will focus on the coding side of the scheduler tasks.

We will concentrate on the following types.
• Schedule with Fixed Delay
• Schedule with Fixed Rate
• Schedule with Cron Trigger

Before jumping into the above scheduling types, we will have a brief look at ThreadPoolTaskScheduler. ThreadPoolTaskScheduler is a simpler way to manage and schedule tasks. It implements Spring’s TaskExecutor interface. Here, the single instance is used for asynchronous execution and scheduled tasks.

Schedule with fixed delay
There are two different ways to schedule Runnable task with fixed delay.

• Fixed delay after the completion of last execution.

• Fixed delay after a specific date.

Schedule with fixed rate: –
Even here, there are two different ways to schedule tasks with fixed rate.

• Scheduling with a fixed rate

• Scheduling with a fixed rate after a specific date.

Schedule with cron trigger
CronTrigger is a bit complex in nature. It helps to schedule a task based on a cron expression.

The above job will run 6AM every morning as per the cron expression.

Conclusion: –
This article is all about the concepts behind Spring’s TaskExecutor and TaskScheduler. It describes all the important points and their implementation details. This brief overview will give you an idea and help you implement it in your project.


Please enter your comment!
Please enter your name here