Spring Scheduled Tasks – 3 Ways To Use Spring Scheduler

In this article, we will see how to run any task or a program using Spring Scheduled Tasks. As usual, we will demonstrate this with our favorite Spring Boot

Softwares used

  • Spring Boot 1.5.9.RELEASE
  • Java 8
  • Maven
  • Eclipse

Spring provides us with convenient @Scheduled annotation. Using this you can turn any methods of your class to a Spring Scheduled Tasks

Spring Scheduled Tasks Mandates

When scheduling tasks with Spring @Scheduled annotation you need to remember and follow a couple of things:

  • The method you want to schedule a task should not accept any parameters
  • The method should not return anything i.e. return type should be void
  • You need to specify one of the methods from cron, fixed-rate or fixed delay to schedule your task.

Let’s dive into an actual example and see one by one each of the ways we can schedule a task.

Enabling Spring Scheduled Tasks

To enable the scheduling you need to let Spring know that with help of @EnableScheduling also on the class in which we add our scheduled method we need to annotate with @Component.

1. Fixed Rate Scheduled Task

As shown in the snippet above you can schedule a task running after a certain interval. The interval is provided in milliseconds.

Now in the above example, we have hardcoded the interval. What if we want to externalize it? Well, we do have an option for that. Just define your interval as:

and define a property batch.fixedrate in your application properties file.

One important thing to note here is that in this method, the next execution of the task does now wait to check if earlier execution has finished or not.

2. Fixed Delay Scheduled Task

In this approach,  the time between tow executions is fixed. The next execution waits to for first is completed its run.

To externalize the configuration you can use:

3. Scheduled Task with Initial Delay

In all the above examples, the task starts right after your application is up and running. If you want to run your task say after 5 minutes of application start what you would do? The Spring has the configuration for that as well.

4. Cron Scheduled Tasks

Sometimes you can not achieve the fine-grained scheduling with the help of fixed-rate or delay. In such scenarios, you can use cron expression to schedule your task.

The above task runs every 10 seconds after the application is up.

To achieve externalization use:

Summary

In this article, we have seen and understood how we can leverage Spring scheduling capabilities. We have learned different ways to configure scheduled tasks.

Please feel free to comment or ask a question or two.

You may also like:

WebSocket Application With Spring Boot Step By Step – Source Code On GitHub

Simple and Easy way to connect MongoDB Atlas with Spring Boot

Steps to develop Spring Batch jobs using Spring Boot with example

Simplest and Easy way to Upload and Download Files in Java with Spring Boot

5 Comments
  1. May 14, 2019 | Reply
  2. June 14, 2018 | Reply
    • Pavan
      June 14, 2018 | Reply
  3. Vadivelan
    April 2, 2018 | Reply
    • Pavan
      April 3, 2018 | Reply

Add a Comment

Your email address will not be published. Required fields are marked *