Spring Book – Chapter 19 – Tasks and Scheduling

TaskExecutor Implementations

Spring has built-in implementations of TaskExecutor interface for almost all the common use case that you would require in your application. If you do have some very specific application requirement, you have a provision of implementing TaskExecutor implementation making use of these built-in implementations as example. The various implementations which Spring provides are as follows:

  • SimpleAsyncTaskExecutor
  • SyncTaskExecutor
  • ConcurrentTaskExecutor
  • SimpleThreadPoolTaskExecutor
  • ThreadPoolTaskExecutor
  • TimerTaskExecutor
  • WorkManagerTaskExecutor

Spring’s TaskScheduler

Similar to TaskExecutor, org.springframework.scheduling.TaskScheduler interface offers additional methods by which tasks can be scheduled to operate sometime in the future. Listing 20-12 shows the TaskScheduler interface signature.

Listing 20-12. TaskScheduler interface signature

This interface is roughly equivalent to a JSR-236 ManagedScheduledExecutorService as supported in Java EE 6 environments. Before going into the details of implementation classes of this interface, I would like to introduce you to certain important classes and interfaces. In the TaskScheduler interface, you would have noticed use of another interface namely org.springframework.scheduling.Trigger.

Trigger Interface

If execution times for a scheduled task can be determined based on past outcomes and arbitrary conditions, this interface can be used. Listing 20-13 shows the Trigger interface signature.

Listing 20-13. The Trigger interface signature

If you want your scheduled task’s execution time on its preceding execution information, you can use the org.springframework.scheduling.TriggerContext interface to all the details. This object encapsulates last execution times and last completion time of a given task. Listing 20-14 shows the TriggerContext interface signature.

Listing 20-14. The TriggerContext interface signature

Spring Framework provides two important Trigger implementations out-of-box which are valuable for various use cases. They are:

  • CronTrigger
  • PeriodicTrigger

TaskScheduler Implementations

Spring has built-in implementations of TaskScheduler interfaces which are unique in their own ways and help in achieving common use cases arising in an application. They are:

  • ConcurrentTaskScheduler
  • ThreadPoolTaskScheduler
  • TimerManagerTaskScheduler

Page Visitors: 25295

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 *