Cipous
Cipous

Reputation: 992

Spring amqp listener thread recovery

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?

Upvotes: 2

Views: 1023

Answers (1)

Gary Russell
Gary Russell

Reputation: 174574

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

Upvotes: 2

Related Questions