开发者

Best way to force Spring shutdown from a bean?

My application uses a Spring Def开发者_开发技巧aultMessageListenerContainer 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?


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

Another option is to inject (or autowire with @Autowired) the application context:

@Inject
private ApplicationContext ctx;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜