0
我试图保留我的应用程序存活以便倾听从我的queue
的一些消息。但是,一旦我的应用程序收到SIGTERM
,我想确保我的应用程序很好地关闭。这意味着,确保内部工作在关闭之前先完成。如何在收到SIGTERM后正确关闭Spring Bean?
阅读之后,我想出了这个:
@Component
public class ParserListenerStarter {
public static void main(final String[] args) throws InterruptedException {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ParserReceiveJmsContext.class);
context.registerShutdownHook();
System.out.println("Listening...");
Runtime.getRuntime().addShutdownHook(//
new Thread() {
@Override
public void run() {
System.out.println("Closing parser listener gracefully!");
context.close();
}
});
while (true) {
Thread.sleep(1000);
}
}
}
然后我送kill
命令我的应用程序;这是我的输出:
Listening...
// output from my other beans here
Closing parser listener gracefully!
Process finished with exit code 143 (interrupted by signal 15: SIGTERM)
从我的豆子的shutdown
方法还不能称作:
@PreDestroy public void shutdown() {..}
我不是Spring
的专家,所以我现在没有任何愚蠢的一点遗憾的是,我在这里失踪了。
我怎么能shutdown
我的豆,然后关闭我的应用程序很好?
你说得对尼古拉!非常感谢您的回答! –