How do I know that my message was sent successfully with spring amqp?
I am writing to a RabbitMQ queue with spring amqp using the RabbitTemplate class. I use the convertAndSend method to send messages to the queue. This works well under normal situations, but it seems to fail silently if the queue doesn't exist. No exception is thrown and no error/debug message is logged to the logger.
What is the best way for me to make sure the message was delivered?
Here is an example of what the code is doing currently.
RabbitTemplate template = new RabbitTemplate(factory);
template.setQueue(queueName);
template.setRoutingKey(queueName);
template.convertAndSend(message);
It's not an error for a RabbitMQ client to send a message to a broker which has no queues bound within it to accept the message. RabbitMQ will silently drop it and the client will be none the wiser. If you've no queues interested in your messages, the broker has not got many other options available to it.
That said, you can make RabbitMQ complain if the message will end up silently dropped by setting the mandatory flag. I don't know if the Spring AMQP interfaces support it, but it's certainly available in the RabbitMQ Java client library.
您可以使用mandatory
并且还可以启用发布商退货(对于无法投递的消息)。