Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Scheduler stops unexpectedly

We have a Spring 3 web application on Tomcat 6 that uses several scheduled services via @Scheduled (mainly for jobs that run every night). Now it appears that sometimes (rarely, perhaps once in two months or so) the scheduler thread stops working, so none of the jobs will be executed in the following night. There is no exception or logging entry in our log files.

Has anybody a clue why this is happening? Or how to get more information about this problem?

Is there a way to detect this situation within the application and to restart the scheduler?

Currently we are solving this by having also a logging job that runs every 5 minutes and creates a log entry. If the log file stops being updated (monitored by nagios), we know it is time to restart tomcat. It would be nice to restart the jobs without a complete server restart.

like image 442
obecker Avatar asked Jul 28 '13 14:07

obecker


People also ask

How do I stop a spring scheduler?

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.

How do I run multiple scheduler in spring boot?

We can easily schedule tasks in spring boot by using @Scheduled annotation. And then we need to enable scheduling by adding @EnableScheduling annotation to a spring configuration class. Spring uses ThreadPoolTaskScheduler for scheduled tasks, which internally delegates to a ScheduledExecutorService.

What is the use of ThreadPoolTaskScheduler?

ThreadPoolTaskScheduler is useful for internal thread management, as it delegates tasks to the ScheduledExecutorService, and implements the TaskExecutor interface. A single instance of it is able to handle asynchronous potential executions, as well as the @Scheduled annotation.

What is the use of @EnableScheduling?

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. The @Scheduled annotation is used to trigger the scheduler for a specific time period.


1 Answers

Since this question got so many votes, I'll post what the (probably very specific) solution to my problem was.

We are using the Apache HttpClient library to make calls to remote services in the scheduled jobs. Unfortunately there are no default timeouts set when performing requests. After setting

connectTimeout connectionRequestTimeout socketTimeout 

to 30 seconds the problem was gone.

int timeout = 30 * 1000; // 30 seconds RequestConfig requestConfig = RequestConfig.custom()         .setConnectTimeout(timeout)         .setConnectionRequestTimeout(timeout)         .setSocketTimeout(timeout).build(); HttpClient client = HttpClients.custom()         .setDefaultRequestConfig(requestConfig).build(); 
like image 110
obecker Avatar answered Sep 28 '22 23:09

obecker