Spring 4 @Scheduled stops working
Asked Answered
S

2

7

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

Singlecross answered 30/8, 2015 at 13:28 Comment(5)
Are you running this inside or outside of a container? Also, what version of Java?Vert
Inside the container (Annotation based config, I have @EnableScheduling @EnableAsync). Java version 8Singlecross
have you read? https://mcmap.net/q/280038/-spring-scheduler-stops-unexpectedly/206466Roanna
As stated in the top answer of @xenoterracide's link, what does a kill -3(or use JVisualVM) gives you ? => to see if any threads are stuck somewhereMerovingian
Hi, Actually, the task that the task executor was supposed to run had an infinite loop, that's why it stopped being scheduled. Thank you for all your responses.Singlecross
T
14

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) {};
}
Transformism answered 25/9, 2015 at 11:16 Comment(3)
Unfortunately this fixedRate stops after a few days. I have similar usage to @Singlecross - start and stop logs and call @Async function. Strange...Vinnie
Is there no way to set a timeout on the scheduled task executor?Deicer
@Deicer I guess there is always a possibility to check for a timeout inside the body of the method, isn't there?Transformism
R
3

see whether job is getting hanged or not. incase if it does.. the task will not execute after some time i mean after reaching max pool size. check you implementation code whether threads are released after successful execution.

Robbins answered 25/9, 2015 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.