Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

Spring Top – Temp

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

>> LEARN SPRING
Lightrun – Third Party Code
announcement - icon

Flakiness in REST requests is a common issue. A request can get a 200 OK in one scenario and a 409 next time. Sometimes a request can even succeed and fail intermittently on the same exact request. In short, working over HTTP can be a bit of a mess without solid tooling.

Also, while it’s easy enough to debug these issues locally when developing the application, we’re talking about production here - we can’t afford the downtime while you’re stepping in and out of code. Uptime is kind of the whole point.

With Lightrun, you can get the same level of access you get with a local debugger or profiler - no downtime required. You can add logs, metrics, and snapshots (think breakpoints, but without stopping the running service), in a safe and read-only manner - without redeploying, restarting, or even stopping the running service. Performance and security are maintained throughout the process.

Learn how to debug a live REST API (built with Spring, of course), using Lightrun, in this 5-minute tutorial:

>> Debugging REST Requests in Spring-Based applications using the Lightrun Platform

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:

@Configuration
@ComponentScan(
  basePackages="com.baeldung.taskscheduler",
  basePackageClasses={ThreadPoolTaskSchedulerExamples.class})
public class ThreadPoolTaskSchedulerConfig {

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler
          = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(5);
        threadPoolTaskScheduler.setThreadNamePrefix(
          "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;
    }
    
    @Override
    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:

taskScheduler.schedule(
  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:

taskScheduler.scheduleWithFixedDelay(
  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:

taskScheduler.scheduleWithFixedDelay(
  new RunnableTask("Current Date Fixed 1 second Delay"),
  new Date(),
  1000);

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:

taskScheduler.scheduleAtFixedRate(
  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:

taskScheduler.schedule(
  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:

periodicTrigger.setFixedRate(true);
periodicTrigger.setInitialDelay(1000);

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.

Spring bottom

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

>> THE COURSE
Generic footer banner
Comments are closed on this article!