Scheduling Task In Spring Boot

In this article we will build a simple spring boot application where we will schedule a task that gets triggered automatically at a certain interval. Scheduling is an important concept when we want to accomplish a certain task at a certain time of day, or periodically at a certain interval for example, sending e-mails to employee every end of the month.


Spring has provided @Scheduled annotation along with some trigger parameters to achieve scheduling. You have to annotate a method with @Scheduled annotation and write your scheduling logic.

Criteria for using @Scheduled annotation

  • Scheduling method should have a void return type.
  • Scheduling method should not accept any arguments.

Enable Scheduling

Before start using scheduling, we have to enable it by using @EnableScheduling either in Spring Boot Application class or Configuration classses.

@Scheduled annotation triggering parameters

You can use the properties fixedRate/fixedDelay/initialDelay/cron to provide the triggering information with @Scheduled annotations. They are discussed with sample example below.

we will show the execution time of each scheduled task in following data format.

Schedule a task with Fixed Rate

Use fixedRate if you want to schedule a task to be executed at a fixed interval. Such scheduled task is invoked at the specified interval even if the previous invoked task is not finished.

If you don’t want to hard-code the fixedRate value, fixedRateString option is given. Just write the key-value pair in and read the key using spring expression as follows.

Schedule a task with Fixed Delay

fixedDelay task is executed between the completion of the last invocation and the start of the next invocation. Well, that means if you have scheduled a fixedDelay task for 3 seconds and your task took 4 seconds to execute its complete logic, then next invocation  will be triggered after 7 seconds. So, the fixedDelay parameter counts the delay after the completion of the last invocation.

fixedDelayString is provided if you dont want to hard-code the values.

Schedule a task with Initial Delay

initialDelay parameter can be used along with fixedRate or fixedDelay to delay the first execution of the task with the specified number of milliseconds.

Schedule a task with Cron Expression

If you want to have more control over scheduling of a task, then you can make use of cron property provided by @Scheduled annotation. As Oracle Documentation states

A cron expression is a string consisting of six or seven sub-expressions (fields) that describe individual details of the schedule.

Each sub-expressions/fields, separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field. Refer the following table given in the Oracle Site for reference.

Allowed Fields and Values in Cron Expressions

Cron Expression Examples

Example : Fire at 10:15 AM every day
If you wish not to hard-code the expression, you can configure the value in file and read the key as below.

Full Example :

Create Spring Boot Project

Create spring boot application from Spring Initializr. Provide the project related meta-data and dependencies as shown below. Click on Generate Project, a zip file will be downloaded. Extract it in your preferred location and import in the your preferred IDE. Here we are using Eclipse Oxygen.

Project Structure

Following is the structure in the project explorer of eclipse IDE that we are going to develop.

Project Dependencies : pom.xml
Spring Boot Application class
Scheduling Task with @Scheduled Annotation : Don’t hard-code your values in code

If you don’t want to hard-code the triggering parameters in @Scheduled annotation then write those values in and read it with the help of Spring Expression as mentioned earlier.
cron.expression=0 15 10 * * ? *

Run the above application and see the execution time of each scheduled task. I hope this article has helped you feel confident to use @Scheduled annotation as per your need. Happy Coding…!!


6 thoughts on “Scheduling Task In Spring Boot”

  1. Amit Sharma says:

    Thank you. I’ve been looking for this .very well documented .

    1. Chandra Mani Gupta says:

      Thanks bro. Do comment in case of any problem.

  2. Gautam says:

    Nicely explain!!

    1. Chandra Mani Gupta says:

      Thanks Gautam for the nice words. Kindly subscribe our blog for similar articles.

  3. Jitesh Kumar says:

    Very useful article …. thanks

    1. Chandra Mani Gupta says:

      Kindly subscribe this blog for more updates.

Leave a Reply