gpt4 book ai didi

java - Spring amqp监听线程恢复

转载 作者:行者123 更新时间:2023-11-30 01:53:29 25 4
gpt4 key购买 nike

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

让我们有代码:

@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 如何监视监听器线程,并且在“消失”的情况下它会重新启动?

或者至少有可能在出现异常时停止整个应用程序吗?

最佳答案

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

关于java - Spring amqp监听线程恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com