Spring amqp listener thread recovery

岁酱吖の 提交于 2021-02-10 08:01:45

问题


I am trying to avoid situation, when thread reading messages from queue is dead, but application is up and running, so its hard to detect problem.

Lets have code:

@RabbitListener(queues = "${enc-correlation.correlation-request-queue}")
public void takeIndexTask(@Payload ConversationList list) throws InterruptedException {
   //just simulation of failure
   throw new OutOfMemoryError(); 
}

This will end up with application running, but not processing new messages.

I tried jvm parameter:

-XX:OnOutOfMemoryError="kill -9 %p"

which did not stop the application. Is it because it is inside of thread?

So only solution, which is ugly and is working is:

    Thread.setDefaultUncaughtExceptionHandler((thread, t) -> {
        if (t instanceof OutOfMemoryError) {
            System.exit(1);
        }
    });

Is there a way, how spring amqp would be monitoring listeners threads and in case of "dissapearing" it would start new?

Or at least is there a possibility to stop whole application in case of some exception?


回答1:


Add an ApplicationListener<ListenerContainerConsumerFailedEvent> bean or an event listener method...

@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());
    }

}

and

Consumer thread died with java.lang.OutOfMemoryError


来源:https://stackoverflow.com/questions/55263378/spring-amqp-listener-thread-recovery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!