Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 @Scheduled stops working

I am using @Scheduled annotation to run a cron job. The scheduling works for some time, and then stops working. I will give simplified snippets of my code:

This is the scheduler:

//org.springframework.scheduling.annotation.Scheduled
@Scheduled("*/30 * * * * *")    
public void performTask() {
    logger.info("Starting agent");
    getAgentAsyncTask().execute();
    logger.info("Ending agent");
}

This is the task which is executed by scheduler

//org.springframework.scheduling.annotation.Async
@Async(TASK_EXECUTOR)
@Override
public void execute() {
    logger.info("Starting task");
    //send some rest requests
    logger.info("Ending task");
}

Both: "Starting agent" and "Ending agent" are logged equal number of times. So, each scheduling is ending properly.

Both: "Starting task" and "Ending task" are logged equal number of times. So, definitely, "task" is not blocking things.

But it just stops logging after some time. What might be the issue?

Here, TASK_EXECUTOR is the following bean:

 @Bean(TASK_EXECUTOR)
 public ThreadPoolTaskExecutor createDefaultTaskExecutor() {
          ThreadPoolTaskExecutor te = new ThreadPoolTaskExecutor();
          te.setMaxPoolSize(15);
          te.setCorePoolSize(15);
          te.initialize();
          return te;
    }

Spring version:

4.1.6.RELEASE

like image 968
coolscitist Avatar asked Aug 30 '15 13:08

coolscitist


People also ask

What is @scheduled in spring?

Spring Core. Spring provides excellent support for both task scheduling and asynchronous method execution based on cron expression using @Scheduled annotation. The @Scheduled annotation can be added to a method along with trigger metadata.

What is @scheduled annotation in spring?

The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.

Is @scheduled asynchronous?

There is no need to use @Async. Just use fixedRate attribute of @Scheduled instead of fixedDelay. Spring will make another invocation on the method after the given time regardless of any call is already being processed.

How do I stop a scheduled spring job?

Another way to stop the scheduler would be manually canceling its Future. In the cases with multiple scheduler tasks, then we can maintain the Future map inside of the custom scheduler pool but cancel the corresponding scheduled Future based on scheduler class.


1 Answers

Such a situation might be caused by an infinite loop in the body of the scheduled method or if there is a call to an external system and the control waits synchronously to receive the response without any timeout.

Try it by yourself with this simple code snippet. The method will be started only once and will not be started after the specified interval of 5 seconds.

@Scheduled(fixedRate = 5000)
public void printPeriodically() {
    System.out.println("This is my periodic method");
    while(true) {};
}
like image 97
Jagger Avatar answered Sep 19 '22 00:09

Jagger