Spring amqp监听线程恢复

Spring amqp listener thread recovery

我正在努力避免这种情况,当从队列中读取消息的线程已死,但应用程序已启动并且 运行,因此很难检测到问题。

让我们有代码:

@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 将如何监视侦听器线程,如果是 "dissapearing",它将如何启动新线程?

或者至少有可能在某些异常情况下停止整个应用程序?

添加 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.OutOfMemoryError