How to Schedule Task With Spring Boot ?

Many times we are in need to run specific task at fixed interval while working with Spring Boot applications. Its also called as Cron Job. In this article we will see How to create Cron Job in Spring Boot ?

There are couple of annotations will get used in this.
1) @EnableScheduling
2) @Scheduled

Now lets get started and we have basic Spring Boot application with us in running state with required dependencies in pom.xml. For scheduling in Spring Boot we do not need any other dependencies other than we have for Spring Boot App.

So lets jump to our main class of Spring Boot App. Add @EnableScheduling above your main class. This will enable scheduler behavior in your Spring Boot application. Without this annotation Scheduler will NOT work in Spring Boot Application.

Now create separate class to add our business logic that we want to run at fixed interval as part of our scheduler. Let's say task() method has business logic to run at fixed interval. Here we will just print current date at every one minute which is fixed interval in our example.

Now add @Scheduled above task() method. So at every one minute task() method will get called by Spring as we have placed @Scheduled on top of it.

Now we need to define interval. In our case its one minute. There are two ways to do it -
1) Using fixedDelay
2) Using Cron Expression

Let's see each of above two ways one by one.

1) Using fixedDelay
In fixed delay we need to provide milliseconds for fixedDelay attribute in @Scheduled. So it is 60000 for 1 minute. So below is complete code for our SchedulerExample class.

import java.util.Date;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class SchedulerExample {

 @Scheduled(fixedDelay = 60000)
 public void task() {
  System.out.println(new Date());
 }
 
}

So this will print current date at every one minute. Just run your Spring Boot Application and wait for 2-3 minutes and it will print current date for 2-3 times.

2) Using Cron Expression
To create cron expression there is good website to refer and that is Cron Maker
Once you open this website you will see different tabs like Minutes, Hourly, Daily etc. So go to Minutes tab and enter value as 1 and click on Generate Cron Expression. After that in Result section you will get cron format like 0 0/1 * 1/1 * ? *

Here 1/1 in cron expression indicates that it should run at every minute. * After that indicates second and it does not matter in our case.

Now in @Scheduled for cron attribute we need to pass this cron expression. But wait we can not pass it as it is. We need to remove right most * from this and then provide cron expression for cron attribute as seen in below code. So final cron expression is 0 0/1 * 1/1 * ?

import java.util.Date;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class SchedulerExample {

 @Scheduled(cron = "0 0/1 * 1/1 * ?")
 public void task() {
  System.out.println(new Date());
 }
 
}

This will also give same result as we have seen in case of fixedDelay. It will print current date at every one minute.

Now you may come up with question as When to use fixedDelay and when to use cron expression.
Now cron expression comes with some flexibility. Like you want to run some task at every day but at specific time. For example every day at 12:10 AM you want to perform some task then we can modify our cron expression and it will become like 0 10 0 1/1 * ?

To generate this you can go to Daily tab in cron maker and put 1 value as you want to run at every day once. Then in Start Time drop downs put value as 00 and 10 respectively. This means every day at 12:10 AM it will run.

You can put this cron expression and try yourself.
Another thing is that in cron maker you can validate and check run times of your cron expression as well. Put your cron expression  and click on Calculate next dates and it will give you date and time this scheduler will run.

So this is how you can have scheduler in Spring Boot Application.

Here we have seen Cron Job / Scheduler with Spring Boot. If you want to know how to create Cron Job / Scheduler using Core Java then you can checkout - Cron Job Using Core Java

0 Comments