【问题标题】:Spring amqp listener thread recoverySpring amqp监听线程恢复
【发布时间】:2019-03-20 14:38:19
【问题描述】:

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

让我们有代码:

@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 将如何监视侦听器线程并在“消失”的情况下重新开始?

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

【问题讨论】:

  • >start new 一般来说,OOM 应该被视为致命,JVM 应该被杀死并重新启动。容器无法做出该决定,因此您可以通过接收事件来做出决定。看我的回答。
  • 好的,我会按照你下面的建议去做。

标签: java spring-amqp


【解决方案1】:

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

【讨论】:

  • 所以我将尝试通过调用 System.exit(1) 来停止此侦听器中的系统;
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
相关资源
最近更新 更多