Partner – Microsoft – NPI (cat= Spring)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

1. Overview

In this tutorial, we’ll discuss the Spring task scheduling mechanism, TaskScheduler, and it’s pre-built implementations. Then we’ll explore the different triggers to use. To read more about scheduling in Spring, can check out these @Async and @Scheduled articles.

Spring 3.0 introduced TaskScheduler with a variety of methods designed to run at some point in the future. TaskScheduler also returns a representation object of the ScheduledFuture interface, which we can use to cancel scheduled tasks and check to see if they’re done or not.

All we need to do is select a runnable task for scheduling, then select a proper scheduling policy.

2. ThreadPoolTaskScheduler

ThreadPoolTaskScheduler is useful for internal thread management, as it delegates tasks to the ScheduledExecutorService, and implements the TaskExecutor interface. A single instance of it is able to handle asynchronous potential executions, as well as the @Scheduled annotation.

Let’s define the ThreadPoolTaskScheduler bean at ThreadPoolTaskSchedulerConfig:

public class ThreadPoolTaskSchedulerConfig {

    public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler
          = new ThreadPoolTaskScheduler();
        return threadPoolTaskScheduler;

The configured bean threadPoolTaskScheduler can execute tasks asynchronously based on the configured pool size of 5.

Note that all ThreadPoolTaskScheduler related thread names will be prefixed with ThreadPoolTaskScheduler.

Let’s implement a simple task we can then schedule:

class RunnableTask implements Runnable{
    private String message;
    public RunnableTask(String message){
        this.message = message;
    public void run() {
        System.out.println(new Date()+" Runnable Task with "+message
          +" on thread "+Thread.currentThread().getName());

We can now schedule the scheduler to execute this task:

  new Runnabletask("Specific time, 3 Seconds from now"),
  new Date(System.currentTimeMillis + 3000)

The taskScheduler will schedule this runnable task at a known date, exactly 3 seconds after the current time.

Now let’s go a bit more in-depth with the ThreadPoolTaskScheduler scheduling mechanisms.

3. Schedule Runnable Task With Fixed Delay

We can schedule a fixed delay with two simple mechanisms:

3.1. Scheduling After a Fixed Delay of the Last Scheduled Execution

Let’s configure a task to run after a fixed delay of 1000 milliseconds:

  new RunnableTask("Fixed 1 second Delay"), 1000);

The RunnableTask will always run 1000 milliseconds later between the completion of one execution and the start of the next.

3.2. Scheduling After a Fixed Delay of a Specific Date

Let’s configure a task to run after a fixed delay of a given start time:

  new RunnableTask("Current Date Fixed 1 second Delay"),
  new Date(),

The RunnableTask will be invoked at the specified execution time, which encompasses the time in which the @PostConstruct method starts, and subsequently with 1000 milliseconds delay.

4. Scheduling at a Fixed Rate

There are two simple mechanisms for scheduling runnable tasks at a fixed rate.

4.1. Scheduling the RunnableTask at a Fixed Rate

Let’s schedule a task to run at a fixed rate of milliseconds:

  new RunnableTask("Fixed Rate of 2 seconds") , 2000);

The next RunnableTask will always run after 2000 milliseconds, regardless of the status of the last execution, which may still be running.

4.2. Scheduling the RunnableTask at a Fixed Rate From a Given Date

taskScheduler.scheduleAtFixedRate(new RunnableTask(
  "Fixed Rate of 2 seconds"), new Date(), 3000);

The RunnableTask will run 3000 milliseconds after the current time.

5. Scheduling with CronTrigger

We use CronTrigger to schedule a task based on a cron expression:

CronTrigger cronTrigger 
  = new CronTrigger("10 * * * * ?");

We can use the provided trigger to run a task according to a certain specified cadence or schedule:

taskScheduler.schedule(new RunnableTask("Cron Trigger"), cronTrigger);

In this case, the RunnableTask will be executed at the 10th second of every minute.

6. Scheduling With PeriodicTrigger

Let’s use PeriodicTrigger for scheduling a task with a fixed delay of 2000 milliseconds:

PeriodicTrigger periodicTrigger 
  = new PeriodicTrigger(2000, TimeUnit.MICROSECONDS);

The configured PeriodicTrigger bean is used to run a task after a fixed delay of 2000 milliseconds.

Now let’s schedule the RunnableTask with the PeriodicTrigger:

  new RunnableTask("Periodic Trigger"), periodicTrigger);

We can also configure PeriodicTrigger to be initialized at a fixed rate, rather than a fixed delay. Furthermore, we can set an initial delay for the first scheduled task by a given milliseconds.

All we need to do is add two lines of code before the return statement at the periodicTrigger bean:


We used the setFixedRate method to schedule the task at a fixed rate, rather than with a fixed delay. Then we used the setInitialDelay method to set an initial delay for the first runnable task to run.

7. Conclusion

In this brief article, we learned how to schedule a runnable task using the Spring support for tasks.

We demonstrated running the task with a fixed delay, at a fixed rate, and according to a specified trigger.

As always, the code is available as a Maven project over in GitHub.

Course – LS (cat=Spring)

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.