Chapter 5. Message Delivery and Acceptance
5.1. The Lifecycle of a Message
5.1.1. Message Delivery Overview
The following diagram illustrates the message delivery lifecycle.
Figure 5.1. Fanout Exchange
A message producer generates a message. A message is an object with content, a subject, and headers. At the minimum, a message producer will produce a message with message content.
The message producer may send the message to the broker and let the routing be taken care of by the properties of the message or by the address of the sender object used to send the message (1).
Or the message producer may set the
message.subject
, which acts as the routing key (2), and then send the message to the broker (3).
Consumers subscribed to exchanges (which uses a temporary, private queue in the background) receive messages when they are connected (4).
Messages are buffered in queues that are subscribed to exchanges (5). Consumers can subscribe to queues and receive messages that were buffered while the consumer was disconnected (6). These queues can also be used to share responsibility for messages between consumers.
5.1.2. Message Generation
The
Message
object is used to generate a message.
- Python
import sys from qpid.messaging import * ... msg = Message('This is the message content') msg.content = 'Message content can be assigned like this' msg.properties['header-key'] = 'value' tx = ssn.sender('amq.topic') # msg.subject set by sender for routing purposes tx.send(msg) msg.subject = 'Messaging Routing Key can also be manually set' # beware that this will interfere with sender-object-based routing
5.1.3. Message Send over Reliable Link
When sent over a reliable link:
- The sender passes the message to the broker.
- The broker responds with an acknowledgement that it takes responsibility for delivery of the message.
- The sender deletes its local copy of the message.
In synchronous operation the thread is blocked while this acknowledgement round-trip occurs. When sending using asynchronous operation, the acknowledgement and deletion is performed in the background, and sent but unacknowledged messages are buffered in the sender replay buffer until they are acknowledged.
5.1.4. Message Send over Unreliable Link
When sent over an unreliable link:
- The sender passes the message to the broker.
- The sender deletes the local copy of the message.
Messages may be lost between the sender and the broker in this mode.
5.1.5. Message Distribution on the Broker
When the broker receives a message, it examines the message and the routing information associated with it to determine how to deliver it.
The bindings on the exchanges that receive the message are examined, and when there is a match between the message and a binding, the message is delivered to any queue with that binding.
5.1.6. Message Receive over Reliable Link
When a message is received over a reliable link:
- The broker passes the message to the receiver.
From this point a number of possibilities exist when the receiver is an acquiring consumer:
- The receiver acknowledges responsibility for the message. In this case the broker deletes the server-side copy of the message.
- The receiver rejects the message. In this case the broker routes the message to an
alternate-exchange
if one is defined for the queue, or else discards the message. - The receiver releases the message. In this case the broker returns the message to the queue with a message header
redelivered:true
. - The receiver disconnects without acknowledging or rejecting the message. In this case the broker returns the message to the queue with a message header
redelivered:true
.
5.1.7. Message Receive over Unreliable Link
When a message is received over an unreliable link:
- The broker passes the message to the receiver.
- The broker deletes the server-side copy of the message.
There is no opportunity for the receiver to reject the message, and no opportunity for the broker to redeliver it when using an unreliable link.