Graceful shutdown spring boot(After completeing queued requests) - spring-boot

I have a spring boot application running on production server.
Now I want to kill the spring boot application but want to make sure that all the queued requests / requests which are still in processing state gets completed?
So can I achieve this?

Related

Websocket server reads messages like 10 min late with Spring boot stomp

My application consists of a master spring boot application and 18 agents spring boot application. Each agent sending information to master for every 30 sec. However after some time master application processes messages that arrived like 10min before. when I restart master application, it works fine. I could not solve the problem. is anyone has idea what should I check

How does Spring Boot app exit after it runs a Cloud Task?

Related to this article:
https://www.baeldung.com/spring-cloud-task
and this example:
https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/timestamp
How does the Spring Boot app exit after it runs the task? Where is the code/configuration to tell the Spring Boot application that once the task is finished it should shut down gracefully?
I'm looking at the Timestamp example:
https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/timestamp/src/main/java/org/springframework/cloud/task/timestamp/TaskApplication.java
which runs the task, prints the timestamp, and then shuts down, but I'm not understanding how the application (Spring Boot) shuts down after the task completes?
A JVM will automatically shut down when there are no daemon threads running. So with Spring Cloud Task (and any Java application), once your processing is complete, unless you have another non-daemon thread running, the JVM will automatically terminate.

spring-boot graceful shutdown

Is there a way in spring boot to control the graceful shutdown of the app.
I know that you can have #PreDestroy methods in beans but how can you control the ordering in which those #PreDestroy methods are called.
You can have multiple beans depending on each other will the shutdown of the context look for this dependency already and call the #PreDestroy methods in the right order or not?
For example what I would like to accomplish is:
1.) stop listening for new requests on rest endpoints
2.) prevent rabbit message listeners to accept new messages
3.) wait for all processing that has started before the shutdown but is not finished yet.
Spring-boot-2-3-0 has added support for graceful shutdown.
you can enable graceful shutdown by setting up server.shutdown=graceful property
To configure the timeout period you can use
spring.lifecycle.timeout-per-shutdown-phase=20s
spring boot documentation
If you can not upgrade to spring boot 2.3 then you can check below project
https://github.com/gesellix/graceful-shutdown-spring-boot

spring-boot 2 graceful shutdown web

Is there any recommended way to gracefully shutdown a Spring:boot 2 app in Kubernetes.
Catch a termination signal SIGTERM
Tell Tomcat to stop taking new requests. (or Jetty, Undertow or Netty/WebFlux depending on the embedded web server used). Or tell SCS to stop sending/listening for messages on Kafka.
Tell Actuator health endpoint to go SERVICE_UNAVAILABLE (503)
And then after a X seconds shutdown the application or (SIGKILL)
I'm trying to do a graceful shutdown Rest apps and SCS (kafka consumer&producer) apps
If you are on the latest version of spring-boot i.e. 2.3.5.RELEASE, then all you need to do it add the below properties to the application.properties file and you are done.
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30s
In Kubernetes world, you can use the preStop hook. But use this when you actually want a hold before SIGTERM is initiated.
This hook is called immediately before a container is terminated. No parameters are passed to the handler. This event handler is blocking, and must complete before the call to delete the container is sent to the Docker daemon. The SIGTERM notification sent by Docker is also still sent. A more complete description of termination behavior can be found in Termination of Pods.

Jersey Rx client jersey-client-async-executor shutdown

The web application [ROOT] appears to have started a thread named [jersey-client-async-executor-0] but has failed to stop it.
How to gracefully shutdown the jersey-client-async-executor?
This is used with Spring Boot, JerseyRxClient with embedded tomcat.
Jersey Gracefully terminates the thread once the request is complete.
I am using spring boot scheduler to make asynchronous request using rx jersey client.
I had the same doubt as everytime a scheduler runs, jersey client creates new threads incrementing the number.
To be sure that the thread is terminated,
Simple Test:
In your subscriber,
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
this will not list the jersey-client-async-executor-* which was used to make the request.

Resources