Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@xxxxxxxx has been closed already

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.

like image 226
APURVA RAKE Avatar asked Dec 03 '25 09:12

APURVA RAKE


1 Answers

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.

like image 88
theonlyrao Avatar answered Dec 05 '25 00:12

theonlyrao