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
1 2 3 4 5 6 7 8 |
public interface TaskScheduler { ScheduledFuture schedule(Runnable task, Trigger trigger); ScheduledFuture schedule(Runnable task, Date startTime); ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period); ScheduledFuture scheduleAtFixedRate(Runnable task, long period); ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay); ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay); } |
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
1 2 3 |
public interface Trigger { Date nextExecutionTime(TriggerContext triggerContext); } |
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
1 2 3 4 5 |
public interface TriggerContext { Date lastScheduledExecutionTime(); Date lastActualExecutionTime(); Date lastCompletionTime(); } |
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
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