tags:

views:

108

answers:

1

My application uses a Spring DefaultMessageListenerContainer to process incoming messages. The main method of the app already registers a shutdown hook.

Question is this: what is the best way to force the application context to shut down?

If I throw a RuntimeException in the message listener, it is handled by the container, and not passed on. Is calling System.exit acceptable? Do I pass along the ApplicationContext to every class that needs to shut down, so I can call close() on it?

A: 

You can cast your application context to ConfigurableApplicationContext and call close() on it. At least that's what happens when the context is shut down in a web-application environment, in cases the servlet context is destroyed.

If you want to get ahold of the ApplicationContext, your bean may implement ApplicationContextAware

Bozho
How does the bean that needs to do the shutdown get the context? It's a POJO after all..
xcut
@xcut it depends on your application. Is it a web application, a desktop application?
Bozho
Neither - a message queuing/routing system. So I guess the only way would be to pass the context to the bean, or implement an additional lifecycle interface of some sort?
xcut
@xcut no, just implement `ApplicationContextAware`
Bozho