问题
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