amqp/rabbitmq 中的立即标志和强制标志有什么区别?

What is the difference between immediate and mandatory flags in amqp/rabbitmq?

我指的是 rabbitmq AMQP-0.9 documentation 的 basic_publish 方法。 它为消息引用了两个标志:- 1) 立即,2) 强制。

mandatory

This flag tells the server how to react if the message cannot be routed to a queue. If this flag is set, the server will return an unroutable message with a Return method. If this flag is zero, the server silently drops the message.

The server SHOULD implement the mandatory flag.

对于即时:-

immediate

This flag tells the server how to react if the message cannot be routed to a queue consumer immediately. If this flag is set, the server will return an undeliverable message with a Return method. If this flag is zero, the server will queue the message, but with no guarantee that it will ever be consumed.

The server SHOULD implement the immediate flag.

这两个标志有什么区别,因为它们看起来一样? 此外,从rabbitmq服务器的角度来看,未传递和未路由的消息有什么区别?

根据您提供的文档摘录:

强制性: 发送到没有匹配绑定到目的地的交换的消息(主要是queue,你需要进一步检查交换之间绑定的情况下会发生什么)将被返回给发布者,这样发布者就会知道代理无法路由消息

立即: 这里更进一步,如果消息没有可用的消费者,发布者会收到通知。

因此,例如,如果您将消息发送到可以将其定向到队列的交换器,但当前队列中没有消费者:

  • 从强制的角度来看一切正常(您的消息已被路由到队列。它不是"lost"
  • 从直接的角度来看,这是不对的,因为在发布时没有消息的消费者。