1. 正常的消息流程
上面这张图,是一个正常的消息从生产到消息流程。在上一篇文章RabbitMQ学习总结(3)-集成SpringBoot中,代码里使用消息确认,消息回退机制,现在详细说一下。
2.1 消息发送确认
消息确认机制,需要实现RabbitTemplate类的一个内部接口ConfirmCallback,这个接口的作用是生产者把消息发送到交换机的结果回调。(Producer -----> Exchange)
参数包括:
CorrelationData correlationData:可以封装业务ID信息,需要在发送消息时传入此参数,这里才能接收到,否则是null
boolean ack:消息发送的结果状态,发送成功是true,失败是false
String cause:发送失败的描述信息,如果发送成功是null。
/** * A callback for publisher confirmations. */ @FunctionalInterface public interface ConfirmCallback { /** * Confirmation callback. * @param correlationData correlation data for the callback. * @param ack true for ack, false for nack * @param cause An optional cause, for nack, when available, otherwise null. */ void confirm(@Nullable CorrelationData correlationData, boolean ack, @Nullable String cause); }
2.2 失败回调
失败回调,需要实现RabbitTemplate类的一个内部接口ReturnCallback,这个接口的作用是消息从交换机到队列的结果回调。(Exchange -----> Queue )。消息从交换到队列失败,失败原因可能是路由键不存在,通道未绑定等等,一般都跟配置有关系。
参数包括:
Message message:发送的消息内容 + 发送消息的配置信息
int replyCode:状态码,发送成功是200
String replyText:发送消息失败的描述信息
String exchange:消息使用的交换机
String routingKey:消息使用的路由键
/** * A callback for returned messages. */ @FunctionalInterface public interface ReturnCallback { /** * Returned message callback. * @param message the returned message. * @param replyCode the reply code. * @param replyText the reply text. * @param exchange the exchange. * @param routingKey the routing key. */ void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey); }
2. 异常消息流程
2.1 消息退回
在消费者接收到消息之后,如果这个消息有问题,或者不是消费者想要的消息,可以把消息退回到原队列。退回原队列要确保有其他的服务可以再次消费这条消息。
参数说明:
long deliveryTag:消息唯一标识,这是RabbitMQ自动生成的,不需要人为管理,只需要从message.getMessageProperties().getDeliveryTag() 就可以获得。
boolean multiple:是否批量退回,不开启就使用false,开启批量退回需要增加自己的业务判断逻辑(比如:攒够几条再批量回退,或者设置等待间隔等等)
boolean requeue:是否退回到消息队列,退回就使用true,就是交给其他消费者处理。
void basicNack(long deliveryTag, boolean multiple, boolean requeue) throws IOException;
2.2 死信交换机
死信交换机,顾名思义,就是不能再使用的消息,都会到这里来。
一般进入死信交换机有3种情况:
1. 消息被退回,又没有被退回到原队列。就是2.1中的requeue参数设置成false的情况。
2. 当消息过期了。队列在创建时如果设置了消息过期时间,消息超过这个时间还没有被消息的情况。
3. 消息超过队列容量。队列设置了最大长度、最大容量,如果超出容量存不下的情况。