Task scheduling in Springboot Application

Tega Isiboge
2 min readDec 10, 2022

--

Pre-requisite

  • Java 1.8
  • IntelliJ IDE or Eclipse
  • Maven
  • Knowledge of how to start a springboot application.

To schedule a function to run in a Spring Boot application, you can use the @Scheduled annotation on the function you want to schedule.

To be able to use the @Scheduled annotation, you would need to resolve the maven dependency that makes this annotation available in your spring boot application.

@Schedule Maven dependency is;

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.2</version>
</dependency>

You can add that to your pom file in your application.

Below we are going to use this annotation inside our Spring boot application main class (driver class).

import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
public class MySpringBootApplication {

@Scheduled(cron = "0 * * * * *")
public void runTaskEveryMinute(){

}

}

In the example above, the runTaskEveryMinutemethod will be executed every minute at the 0th second of every minute. The six fields (“0 * * * * *”) in a cron expression represent the following:

  1. Second (0–59)
  2. Minute (0–59)
  3. Hour (0–23)
  4. Day of month (1–31)
  5. Month (1–12)
  6. Day of week (0–7, where 0 or 7 represents Sunday)

The @Scheduled annotation also supports using fixed-rate or fixed-delay to schedule the method execution. Here’s an example of how to use those options:

// run every 5 seconds
@Scheduled(fixedRate = 5000)
public void runEveryFiveSeconds() {
// code to run here
}

// run every hour at the 30th minute
@Scheduled(cron = "0 30 * * * *")
public void runEveryHourAtTheHalf() {
// code to run here
}

The cron parameter in the @Scheduled annotation specifies a cron expression that determines the schedule for running the task. A cron expression is a string of six or seven fields separated by white space. The fields represent the following:

  1. Second (0–59)
  2. Minute (0–59)
  3. Hour (0–23)
  4. Day of month (1–31)
  5. Month (1–12)
  6. Day of week (0–7, where 0 or 7 represents Sunday)

The cron expression allows you to specify a schedule with much more granularity than the fixedDelay parameter. For example, you could use a cron expression to run a task every weekday at 9am, or every 15 minutes, or any other schedule you can express using the six fields of the cron expression.

In contrast, the fixedDelay parameter in the @Scheduled annotation specifies the time, in milliseconds, to wait between the completion of one scheduled task and the start of the next. This means that the scheduled tasks will be run with a fixed delay between them, regardless of how long each task takes to run.

Here’s an example that uses both the fixedDelay and cron parameters:

@Scheduled(fixedDelay = 5000, cron = "0 0 9 ? * MON-FRI")
public void runEveryWeekdayAt9am() {
// code to run here
}

In this example, the task will run every weekday (Monday through Friday) at 9am, with a delay of 5 seconds between each run.

I hope this is helpful for you.

--

--

Tega Isiboge
Tega Isiboge

Written by Tega Isiboge

A good blend of Software Engineering and Business Management Expertise, helping companies surpass their revenue goals, & achieve strategic business objectives.

No responses yet