Search code examples
javaspringrabbitmqspring-amqp

How do I detect that a RabbitMQ producer failed to send a message to an appropriate Queue in Java using Spring AMQP?


I'm trying to validate whether a sent message was successfully placed in a queue using the Spring AMQP library. I see that the methods don't seem to error when I send a message to a queue which has an incorrectly configured binding in it, is there any built in way, or otherwise reasonable way to check this?

E.g. I create a queue and accidentally create a binding w/ incorrect routing key, I create a publisher and publish using the correct (intended) routing key, > How do I see here at this stage that the messages are failing to publish

I've tried looking for any sort of errors the send method errors on in Spring AMQP to identify this in a try-catch, but it doesn't seem to error when the keys are mismatched and there is no existing queue with a matching key. I've had a look through the other methods, but I'm not seeing an obvious alternative that would accomplish what I am looking for.

My only other thoughts were to publish a message, and then consume it at startup to validate the keys are correct on startup, but I'm not a very big fan of that solution and wanted to know if there was an elegant way or standardish pattern to accomplish this.


Solution

  • See Publisher Confirms and Returns:

    https://docs.spring.io/spring-amqp/reference/html/#cf-pub-conf-ret

    https://blog.rabbitmq.com/posts/2011/02/introducing-publisher-confirms

    See also a RabbitTemplate.setReturnsCallback(ReturnsCallback returnCallback):

    https://docs.spring.io/spring-amqp/reference/html/#publisher-returns