Search code examples
javaspring-amqpspring-rabbit

Catching exceptions from Spring AMQP Rabbit Consumer


I' m trying to create a RPC service using this tutorial.

I would like to catch exceptions. Currently when doJob is throws an exception the producer gets timeout exception and the exception isn't logged on console on server.

I tried with container.setErrorHandler, but no result. How can I catch exceptions both in producer and consumer?

I mean

public String doJob(int clientId) {
    String uuid = UUID.randomUUID().toString();
    log.info("UUID {} generated on node {} upon request from client {}", uuid, nodeId, clientId);
    return uuid;
}

can return Exceptions. How can I catch them in this part:

        String payload = proxy.doJob(clientId);
        log.info("Client {} received payload: {}", clientId, payload);
        Thread.sleep(2000);

Solution

  • You're using a request/response pattern with asynchronous messaging. The producer and consumer are therefore logically separated. This separation is explicit because it has numerous benefits. It is a fundamental tenet of asynchronous messaging.

    Exceptions thrown from the consumer do not propagate back through the message broker to the producer who sent the message. That would defeat the logical separation of clients.

    The timeout exception on the producer indicates that something went wrong with the consumer which prevented it from returning a response. Due to the logical separation of producers and consumers the producer will never know why the timeout exception happened. Again, this is by design.