java.lang.IllegalStateException: AnnotationConfigServletWebServerApplicationContext has been closed already

Viewed 944

I have created a profile which I am using in order to create a YAML file after which I want to shut-down the Spring Boot server completely.

In order to do this I am running SpringApplication.exit() like so:

try {
    // ..
} catch (IOException e) {
    throw new RuntimeException(e);
} finally {
    LOGGER.info("Calling SpringApplication.exit()");
    SpringApplication.exit(appContext, () -> 0);
}

However, I'll get a java.lang.IllegalStateException which is a problem because it causes tests and workflows (Github) to fail. The

java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7b7fdc8 has been closed already
        at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1138) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1153) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.event.ApplicationListenerMethodAdapter.getTargetBean(ApplicationListenerMethodAdapter.java:371) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:336) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:229) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:166) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) ~[spring-context-5.3.10.jar:5.3.10]
        at org.springframework.boot.context.event.EventPublishingRunListener.running(EventPublishingRunListener.java:111) ~[spring-boot-2.5.5.jar:2.5.5]
        at org.springframework.boot.SpringApplicationRunListeners.lambda$running$6(SpringApplicationRunListeners.java:79) ~[spring-boot-2.5.5.jar:2.5.5]
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) ~[na:na]
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117) ~[spring-boot-2.5.5.jar:2.5.5]
        at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111) ~[spring-boot-2.5.5.jar:2.5.5]
        at org.springframework.boot.SpringApplicationRunListeners.running(SpringApplicationRunListeners.java:79) ~[spring-boot-2.5.5.jar:2.5.5]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:353) ~[spring-boot-2.5.5.jar:2.5.5]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.5.jar:2.5.5]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.5.jar:2.5.5]
        at io.meda.spring.Application.main(Application.java:62) ~[main/:na]

Workaround

My current workaround is to wrap SpringApplication.run() and catch this exception like so:

try {
    SpringApplication.run(Application.class, args);
} catch (IllegalStateException e) {
    LOGGER.warn("Got IllegalStateException. Program will exit.", e);
}

However, I'd like to get rid of this and to it the "right way".

Environment

id 'org.springframework.boot' version '2.5.5'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
0 Answers
Related