After restarting a Spring Boot application using actuator, we are not able to start the application.
We have Spring Boot application, where we want to implement a functionality where we can restart the application using actuator. Application is able to run but when we call restart() then it wont up and throw the exception:
"Caused by: java.lang.IllegalStateException: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b84f475 has been closed already"
//Controller Class
@PostMapping("/restartApp")
public void restartUsingActuator() {
restartService.restartApp();
}
//Service Class
@Autowired private RestartEndpoint restartEndpoint;
public void restartApp() {
System.out.println("in restartApp");
ConfigurableApplicationContext ctx = restartEndpoint.restart();
ctx.refresh();
}
Expecting to run the application.
As explained here:
it’s important to recreate the context in a separate non-daemon thread — this way we prevent the JVM shutdown, triggered by the close method, from closing our application. Otherwise, our application would stop
The error you're seeing means the spring application has been stopped, so there is no injected RestartEndpoint service to use to restart the application.
That article should help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With