-
Notifications
You must be signed in to change notification settings - Fork 38.4k
ScheduledAnnotationBeanPostProcessor: graceful shutdown should not interrupt currently running jobs #31019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Along the lines of your programmatic example, have you tried explicitly specifying the scheduler as a bean:
If that works, your problem might rather be in Spring Boot and its properties-configured default TaskScheduler. |
Hi @jhoeller , yes I have, the same problem persists |
So you are calling You could also try to add a |
FYI there is a significant revision of the Nevertheless, the configuration options above should work fine in 6.0.x as well. |
Hi @jhoeller I took a look at the code and to try to spot the differences. As far as I can say the By digging a bit further I could see that |
Thanks, that's very timely insight! Indeed, those |
Thanks, looking forward for the fix to this issue! |
…er ThreadPoolTaskScheduler (#2721) (#2738) There has been a significant revision of `ThreadPoolTaskScheduler/Executor` lifecycle capabilities as part of the spring-6.1.x release. It includes a concurrently managed stop phase for `ThreadPoolTaskScheduler/Executor`, favouring early soft shutdown. As a result, the `ThreadPoolTaskScheduler`, which is used for pubsub publishing, now shuts down immediately on `ContextClosedEvent`, thereby rejecting any further task submissions (#2721). This PR aims to retain the default behavior of spring-6.1.x but provides config options to leverage the `lateShutdown` of the underlying `ThreadPoolTaskScheduler`, such as: `spring.cloud.gcp.pubsub.publisher.executor-accept-tasks-after-context-close=true`. References: 1. spring-projects/spring-framework#32109 (comment) 2. spring-projects/spring-framework@b12115b 3. spring-projects/spring-framework@a2000db 4. spring-projects/spring-framework#31019 (comment) 5. https://github.com/spring-projects/spring-framework/blob/996e66abdbaad866f0eab40bcf5628cdea92e046/spring-context/src/main/java/org/springframework/scheduling/concurrent/ExecutorConfigurationSupport.java#L482 Fixes #2721.
Problem
I'm trying to configure Graceful Shutdown for my application when it receives a SIGTERM event, but I'm getting
InterruptedExceptions
for Scheduled jobs where threads enter the wait status (due to a HTTP call or just a simpleThread.sleep()
invocation).Scenario
I created the example below by generating a project on Spring Initalizr with the following parameters:
From there I changed the
DemoApplication
class as following:Besides that, I created a
Scheduled
job as per the following:And finally configured my
application.properties
as following:Expectations
When running the code above calling the method
waitThatFails
I expect NOT to get the following exception:and I expect it to work the same way when I call instead the method
waitThatWorks
, meaning, the current running job is finished on the event of akill -15 <PID>
and no next jobs are scheduled to execute in sequence, making it safe to tear down the application.Interesting to note that for standard Java applications this behavior can be achieved, please see the following example: https://github.com/schmittjoaopedro/schmittjoaopedro.github.io/blob/gh-pages/assets/other/SOQuestionGracefulShutdown.java
References
https://stackoverflow.com/questions/76868362/spring-graceful-shutdown-not-waiting-for-scheduled-tasks-with-thread-sleep
The text was updated successfully, but these errors were encountered: