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

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.

Related

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, Kotlin, Kafka and shutdown

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?

How to gracefully shutdown the spring boot application

I need to gracefully start and shutdown the spring boot application using a script . Is there any other way to do it except using an actuator module in the application. Currently i have to stop the spring boot process manually in the task manager.
Ultimately the spring boot application spins off a java process which needs to be killed. At present you are killing it manually.
You have a few options:
You can use SpringApplication.exit(ApplicationContext, ExitCodeGenerator...) method.
If your application is not a long running application then do you have some exit point where your application should stop. At that point you can System.exit(0)
You can use external manager tools, for example on unix you can use supervisor, here is a blog you can read about this.
SpringApplication.run gives you ApplicationContext which you can close.

How to gracefully shutdown a Spring Boot application?

Is there any difference between springapplication.registerShutdownhook() () and Springapplication.exit(applicationcontext) ? Will either of them also shut down the JVM gracefully?
If you have a ConfigurableApplicationContext, you can use the method registerShutdownHook() to register a shutdown hook with the JVM runtime. You can see more here.
http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html?is-external=true#addShutdownHook-java.lang.Thread-
You use Springapplication.exit(applicationcontext) to close the application context an finish the Spring application.

Spring Boot (Tomcat) based application as daemon - howto stop?

I wrote a Spring Boot webservice that uses an embedded tomcat as container.
In case the system reboots I want to backup some information to a mysql database.
In my webservice I use #Scheduled() and #PreDestroy to run the backup.
This goes well when I stop the server with ^C.
But when I kill the process with an sysV skript (/etc/init.d) and the kill command - even though the daemon has a dependency on mysql, the mysql server is shut down before the backup is finished (resulting in SQL Exceptions in my log).
The reason for this is of course, that kill only sends a signal to stop the process.
How can I (from my sysv skript) synchroneously stop the running spring boot tomcat server?
If you include spring-boot-starter-actuator then that provides a REST endpoint for management. One of the endpoints provided is /shutdown. By hitting that endpoint, you will get a controlled shutdown of all resources, which ensures that #PreDestroy will be called. As this could be dangerous to have enabled by default, to use it you will need to add the following to your application.properties file:
endpoints.shutdown.enabled=true
Of course, once you have exposed that endpoint you need to ensure that there's a teeny bit of security applied to prevent just anybody shutting down your server.
On a related note, you may find my answer to Spring Boot application as a Service useful, where I provided the code for a full init.d script which makes use of this.
As an alternative to the "/shutdown" endpoint the Actuator also has an ApplicationPidListener (not enabled by default) that you can use to create a pidfile (which is commonly used in "init.d" style scripts to kill a process when you want to stop it). The JVM should respond to a kill (sigint) and Spring will shutdown gracefully.

Resources