springboot-SpringBoot Schedule jobs By Configurations

1. Introduction

This article would demo how to use @Scheduled in SpringBoot with configuration properties.

2. Environments

  • SpringBoot 1.x and 2.x

3. The fixed way to use @Scheduled in SpringBoot apps

By default , we can add scheduling ability to SpringBoot app like this:

@Configuration
@EnableScheduling
public class MyRepeatJobConfig {

    @Scheduled(fixedDelay = 1000) //every 1 second
    public void scheduleFixedDelayTask() throws InterruptedException {
        System.out.println(
                "Fixed delay task - " + System.currentTimeMillis() / 1000);
    }
}

You can see that, the fixedDelay(1000) is fixed by hardcoding in the program, if we want to change it ,we must recompile and redeploy the project.

So, is there any way to change to fixedDelay without changing the code? Yes, you can use the @Configuration to do this job.

4. The Configurable way to use @Scheduled in SpringBoot apps

4.1 Configure scheduling parameter by fixedDelayString

The @Scheduled annotation support to configure the delay period by using fixedDelayString like this:

@Configuration
@EnableScheduling
public class ReadResultJob {
    @Scheduled(fixedDelayString = "${readresult.milliseconds}")
    public void scheduleFixedDelayTask() throws InterruptedException {
        System.out.println(
                "Fixed delay task - " + System.currentTimeMillis() / 1000);
        Thread.sleep(10*1000);
    }
}

It means that if you have configured readresut.milliseconds in your application.properties or application.yml, then it would be used in the @Scheduled annotation.

4.2 Configure scheduling parameter by fixedDelayString and default value

The @Scheduled annotation support to configure the default delay period by using fixedDelayString and a colon like this:

@Configuration
@EnableScheduling
public class ReadResultJob {
    @Scheduled(fixedDelayString = "${readresult.milliseconds:60000}")
    public void scheduleFixedDelayTask() throws InterruptedException {
        System.out.println(
                "Fixed delay task - " + System.currentTimeMillis() / 1000);
        Thread.sleep(10*1000);
    }
}

It means that if you have NOT configured readresut.milliseconds in your application.properties or application.yml, then the value would be 60 seconds.

4.3 Configure scheduling parameter by fixedDelayString, default value and a little tweak

The @Scheduled annotation support to configure the default delay period by using fixedDelayString and a colon like this:

@Configuration
@EnableScheduling
public class ReadResultJob {
    @Scheduled(fixedDelayString = "${readresult.seconds:60}000")
    public void scheduleFixedDelayTask() throws InterruptedException {
        System.out.println(
                "Fixed delay task - " + System.currentTimeMillis() / 1000);
        Thread.sleep(10*1000);
    }
}

It means that if you have configured readresut.seconds in your application.properties or application.yml, then the value would be attached with 000, you do not have to compute it with 1000 anymore.

5. Summary

You can see that schedule a SpringBoot task is very easy, if you use it by configuration, then your app would be very flexible.