Spring Boot Scheduler Job @EnableScheduling example

By | 2018-08-12T16:39:04+00:00 August 13th, 2018|Spring Boot|0 Comments

The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks with the TaskExecutor and TaskScheduler interfaces, respectively.

Spring Boot Scheduler:

In this tutorials, we are going to see how can we use TaskScheduler using Spring Boot Scheduler. And also we will have a discussion on how to use Spring @EnableScheduling and @Scheduled annotations.

Enable Scheduling in Spring Boot:

To enable scheduling tasks in spring boot applicaiton, we can use the java based @EnableScheduling annotation.

SpringBootConfig.java
@Configuration
@EnableScheduling
public class SpringBootConfig {
    // config details..
}

If you are in xml based configuration, you can directly use <task:annotation-driven> in you config xml.

Rules to create schedulers:

  • The scheduler methods should not return any values, that means it should be void.
  • The scheduler methods should not accept any parameters.

Creating Schedule Tasks:

The @Scheduled annotation is used to create a schedule, it can be added to a method along with the data. Here are the different schedules :

Scheduling with Fixed Delay:

@Scheduled(fixedDelay = 6000)

The duration between the end of the end of last task and start of next task is fixed. The next task always waits until the previous one is finished.

ScheduleTasks.java
package com.onlinetutorialspoint;

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

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Scheduled(fixedDelay = 6000)
    public void reportCurrentTime() {
        System.out.println("The time is -: "+ dateFormat.format(new Date()));
    }
}

Scheduling with Fixed Rate:

@Scheduled(fixedRate = 6000)

Fixed Rate makes the task on periodic intervals even if the last invocation may still be running.

ScheduleTasks.java
@Component
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Scheduled(fixedRate = 6000)
    public void reportCurrentTime() {
        System.out.println("The time is -: "+ dateFormat.format(new Date()));
    }
}

Scheduling with Initial Delay:

@Scheduled(fixedRate = 6000, initialDelay = 1000)

An initial delay can be specified indicating the number of milliseconds to wait before the first execution of the method.

ScheduleTasks.java
@Component
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Scheduled(fixedRate = 6000, initialDelay = 1000)
    public void reportCurrentTime() {
        System.out.println("The time is -: "+ dateFormat.format(new Date()));
    }
}

Scheduling using Cron Expression:

Some times these schedulers (delays and rates) are not enough to fulfill our requirement, then a cron expression may be provided.

Let’s take an example, I would like to execute a perticular functionality only on weekdays (MON-FRI). We can define this as following..

Cron Expression
@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
    // do something here..
}

Some of the cron examples listed below:

Cron Expressions
"0 0 * * * *" = the top of every hour of every day.
"*/10 * * * * *" = every ten seconds.
"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
"0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day.
"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.
"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
"0 0 0 25 12 ?" = every Christmas Day at midnight

You can see more details on cron expresions here.

Happy Learning:)

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment