Spring boot @Scheduled annotation example

To schedule job in spring boot application to run periodically, spring boot provides @EnableScheduling and @Scheduled annotations. Lets learn to use Spring boot @Scheduled annotation.

Let’s say you want to run job at every 10 seconds interval. You can achieve this job scheduling in below steps:

1. Add @EnableScheduling to Spring Boot Application class

Add @EnableScheduling annotation to your spring boot application class. @EnableScheduling is a Spring Context module annotation. It internally imports the SchedulingConfiguration via the @Import(SchedulingConfiguration.class) instruction

public class SpringBootWebApplication {

Read More : 4 ways to schedule tasks in Spring

2. Add Spring boot @Scheduled annotations to methods

Now you can add @Scheduled annotations on methods which you want to schedule. Only condition is that methods should be without arguments.

ScheduledAnnotationBeanPostProcessor that will be created by the imported SchedulingConfiguration scans all declared beans for the presence of the @Scheduled annotations.

For every annotated method without arguments, the appropriate executor thread pool will be created. This thread pool will manage the scheduled invocation of the annotated method.

2.1. Schedule task at fixed rate

Execute a task at a fixed interval of time:

@Scheduled(initialDelay = 1000, fixedRate = 10000)
public void run() {
	logger.info("Current time is :: " + Calendar.getInstance().getTime());

Now observe the output in console:

2017-03-08 15:02:55 - Current time is :: Wed Mar 08 15:02:55 IST 2017
2017-03-08 15:03:05 - Current time is :: Wed Mar 08 15:03:05 IST 2017
2017-03-08 15:03:15 - Current time is :: Wed Mar 08 15:03:15 IST 2017
2017-03-08 15:03:25 - Current time is :: Wed Mar 08 15:03:25 IST 2017
2017-03-08 15:03:35 - Current time is :: Wed Mar 08 15:03:35 IST 2017

2.2. Schedule task at fixed delay

Configure a task to run after a fixed delay. In given example, the duration between the end of last execution and the start of next execution is fixed. The task always waits until the previous one is finished.

@Scheduled(fixedDelay = 10000)
public void run() {
	logger.info("Current time is :: " + Calendar.getInstance().getTime());

2.3. Spring boot cron job example

@Scheduled annotation is very flexible and may take cron expression as well.

@Scheduled(cron = "0 10 10 10 * ?")
public void run() {
	logger.info("Current time is :: " + Calendar.getInstance().getTime());

Drop me your questions on this spring task scheduler annotation example.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

16 thoughts on “Spring boot @Scheduled annotation example”

  1. Scheduler is running every 1 min if one process is started it takes more than one min to complete this process mean time it doesn’t start one more process until complete the previous process how can i achieve this one in spring boot .

    • Use:
      @Scheduled(fixedDelay = 60000)

      fixed delay will only start the next process until the previous one is complete, in this case, if previous one is completed before 1 min, it will wait till 1 min and start the next process

  2. For cron job example, is there a way to get all jobs created ? in order to see which one are created and running in runtime. I think the equivalent in shell command would be: crontab -e

  3. Hi Lokesh,

    Does the scheduler stop/fail due to external reasons like thread pool max size reached etc. I ask this because my scheduler is expected to run indefinitely but it stops unexpectedly.

    Lakshmi Venkateswaran.

    • Hi Lakshmi, I have used it in one of my application at employer site and it is working since a year without failure at production. I have not faced any such issue. It seems there is some issue with the task it tries to run. Some StackOverflow threads also suggest so.

      Can you please review the details of your task or else share if need second opinion.

  4. How will i be able to disable a schedular in spring boot,when the uses wisheds to stop it.Is there any example code for this.
    Kindly help!

  5. Hi Lokesh,

    Thanks for this article.

    If I have option of running Linux/Unix cronjob and also Spring scheduler. Which one be preferred option to go for? Any pros and cons?



Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.