Spring boot, Kotlin, Kafka and shutdown - spring-boot

I have a Spring boot / Kotlin application that has a Kafka listener embedded. I am programmatically shutting it down at some point by calling:
configurableApplicationContext.close()
However, Kafka listener does not shut down at that time and instead I get this:
Failed to shut down 1 bean with phase value 2147483547 within timeout of 30000: [org.springframework.kafka.config.internalKafkaListenerEndpointRegistry]
After that, the app shuts down as expected.
What do I need to do differently so it shuts down cleanly when requested?

Related

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.

Camel doesn't terminate Java process with max idle seconds property (Spring-Boot)

I have a Spring boot application that uses Apache camel. It is a standalone application that polls a directory with Camel File Component, processes the content and send it Kafka with Camel Kafka Component. I would like the application to be short lived so I want it to self terminate after given idle seconds. I am using camel-spring-boot-starter dependency so I can setup max idle seconds in application properties:
camel.springboot.duration-max-idle-seconds=20
After 20 seconds I see that the camel context and my webserver(used for actuator) are shutdown gracefully but the java process doesn't return an exit code 0. The process hangs like this forever.
Using a similar property, if I set camel.springboot.duration-max-seconds=20 this time, camel context and the web-server shutdown logs are exactly the same and shuts down correctly and the Java process returns an exit code 0, which I was expecting.
Do you happen to run into a similar behavior? Why duration-max-idle-seconds does a graceful shutdown of camel but doesn't terminate the Java process?
I was facing this issue on Camel 3.2.0 and updating to 3.6.0 solves the problem.

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

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.

How to manually shutdown embedded Tomcat?

I'm trying to implement a graceful shutdown sequence for my Spring Boot application. For that I registered a custom shutdown hook with Runtime and disabled the one provided by Spring (SpringApplication.setRegisterShutdownHook(false)). From this custom shutdown hook I first would like to pause embedded Tomcat or the connectors and some other schedulers after which I manually invoke applicationContext.close() to shutdown the rest of the Spring application.
What is the best way to get access to the embedded Tomcat instance? I was fiddling around with TomcatEmbeddedServletContainerFactory but this does not seem to give me access to default connectors or EmbeddedServletContainer which has a stop method.
You can access the EmbeddedServletContainer from the EmbeddedWebApplicationContext (just inject that) and downcast it.

Resources