Reputation: 13071
I have a Spring web application. When the application context has started I use ApplicationListener<ContextRefreshedEvent>
to perform some application initalization, in particular I load data from a web service. If this web service is not reachable then my application cannot do anything useful and it does not make sense to keep it running. Is there a way to programmatically shutdown the application context?
I have tried to do the following:
if (contextRefreshedEvent instanceof ConfigurableApplicationContext)
{
ConfigurableApplicationContext configurableApplicationContext
= (ConfigurableApplicationContext) contextRefreshedEvent;
configurableApplicationContext.close ();
}
However rather then closing gracefully that produces the following exception:
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
So what is the proper way to shutdown the application in such a situation?
Upvotes: 2
Views: 1580
Reputation: 41
Throw an ApplicationContextException
. It worked for me with Spring 4.2.7.RELEASE
Upvotes: 2