官网文档解释:
If a message is published with the "mandatory" flags set, but cannot be routed, the broker will return it to the sending client (via an AMQP.Basic.Return command).
To be notified of such returns, clients can implement the ReturnListener interface and call Channel.addReturnListener****. If the client has not configured a return listener for a particular channel, then the associated returned messages will be silently dropped.
A return listener will be called, for example, if the client publishes a message with the "mandatory" flag set to an exchange of "direct" type which is not bound to a queue
大意为:开启强制消息投递(mandatory为设置为true),但消息未被路由至任何一个queue,则回退一条消息到生产者,通过配置ReturnListener监听未被路由的消息,如果mandatory为设置为false,那么未被路由选消息将会被丢弃。
channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException { } });
RabbitMQ Java Client