我试图避免这种情况,当线程从队列读取消息是死的,但应用程序正在启动和运行,因此很难检测到问题。
让我们来看看代码:
@RabbitListener(queues = "${enc-correlation.correlation-request-queue}")
public void takeIndexTask(@Payload ConversationList list) throws InterruptedException {
//just simulation of failure
throw new OutOfMemoryError();
}这将导致应用程序运行,但不会处理新消息。
我尝试了jvm参数:
-XX:OnOutOfMemoryError="kill -9 %p"这并没有停止应用程序。是不是因为它在线程内部?
因此,唯一的解决方案,这是丑陋和有效的是:
Thread.setDefaultUncaughtExceptionHandler((thread, t) -> {
if (t instanceof OutOfMemoryError) {
System.exit(1);
}
});有没有办法,spring amqp将如何监控监听器线程,并在“消失”的情况下重新开始?
或者至少有可能在出现某些异常的情况下停止整个应用程序?
发布于 2019-03-20 23:10:42
添加ApplicationListener<ListenerContainerConsumerFailedEvent> bean或事件侦听器方法...
@SpringBootApplication
public class So55263378Application {
public static void main(String[] args) {
SpringApplication.run(So55263378Application.class, args);
}
@RabbitListener(queues = "foo")
public void listen(String in) {
throw new OutOfMemoryError();
}
@EventListener
public void listenForOOMs(ListenerContainerConsumerFailedEvent event) {
System.out.println("Consumer thread died with " + event.getThrowable());
}
}和
Consumer thread died with java.lang.OutOfMemoryErrorhttps://stackoverflow.com/questions/55263378
复制相似问题