【发布时间】:2016-11-18 20:29:30
【问题描述】:
我正在尝试保持我的应用程序活动,以便收听来自我的queue 的一些消息。但是,一旦我的应用程序收到SIGTERM,我想确保我的应用程序能够正常关闭。意思是,确保工作在内部首先完成,然后再关闭。
读完之后,我想出了这个:
@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)
我的 bean 中的 shutdown 方法没有被调用:
@PreDestroy public void shutdown() {..}
我不是Spring 方面的专家,所以对于我在这里遗漏的任何愚蠢观点,我深表歉意。
我怎样才能shutdown我的bean然后很好地关闭我的应用程序?
【问题讨论】: