5

I have a web application which I'm trying to shut down by executing the close() method on the context:

((ConfigurableApplicationContext) appContext).close();

After executing this the application appears to be continuing running, health page produces the following exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)

Is there a way to shudown the whole thing via Spring or should I resort to System.exit()?

Any help would be kindly appreciated!

alexs333
  • 12,143
  • 11
  • 51
  • 89
  • 1
    You should almost never use `System.exit()`. It sounds like you're running the application inside a traditional container instead of using a Spring Boot embedded container. In that case, you'll need to use the container's shutdown command. – chrylis -cautiouslyoptimistic- Mar 16 '15 at 05:07
  • @chrylis yep agree `System.exit()`. I am running the application in traditional in traditional servlet container. Would there be a way to send the shutdown command to tomcat or jetty from my Spring application programmatically? – alexs333 Mar 16 '15 at 05:31
  • I'm not sure. You'd have to be able to get access to the management interface, or to do something like run `tomcat stop` as a process. Is migrating to an embedded servlet container an option? It sounds like your container is a single-application container anyway. – chrylis -cautiouslyoptimistic- Mar 16 '15 at 06:14
  • 1
    It is a option but quite interested to find out if there is a possibility to do that without switching going embedded. – alexs333 Mar 16 '15 at 06:19

1 Answers1

1

PLease have a look at these questions. They are on the same line as yours: How to manage Tomcat via Java, Start / stop a web application from itself?

As @chrylis suggested, I think the easiest way of shutting down the server(if it's not running in embedded mode) is to use Runtime class.

Community
  • 1
  • 1
geekprogrammer
  • 1,108
  • 1
  • 13
  • 39