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.
Related
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.
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
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.
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.
i saw some code use ShutdownHook like this
Runtime.getRuntime().addShutdownHook(new Thread(){
ConfigurableApplicationContext.stop();
//close spring context;
threadpool.shutdownnow();
//close theadpool
});
is there anything useful to do like this?
i thought
when jvm exit ,maybe thread will be shutdown immediately
and spring context will close tooï¼›
what shall we do next when we need to call System.exit() ?
It really depends on your application and the lifecycle of your objects and those threads you appear to have outside of your context. If you are running the spring container inside a standalone java process, then trapping the shutdown hook like this is one way to do that. Another way is to have it listen on a tcp port and send a command to begin the shutdown process. If you are running in a web container like tomcat, then you should follow the standards on normal webapp shutdown, which Spring supports with Context Listeners.
I would also consider redesigning your app so that the threads are all managed with a bean that lives inside your spring container. For instance using a bean that is configured with directives (attributes) for start/stop methods and then that bean would use an Executor for thread pooling. This way, your shutdown is ONLY shutting down the Spring container, and Spring provides very good support for orderly shutdown of beans. One of those beans is your object holding the threads within the Executor. Its a much cleaner way than trying to integrate Spring beans with external threads.
Hope this helps.