1.2. The AMQP 0-10 Model

AMQP defines both a wire level protocol (the transport layer) and higher level semantics for messaging (the functional layer).
In AMQP, a connection represents a network connection, and a session represents the interface between a client and a broker. A session uses a connection for communication. Sessions may be synchronous or asynchronous.
The following diagram shows how the MRG Messaging broker is used by producer-consumer applications. A broker contains exchanges and queues. Message producers write to exchanges, exchanges route messages to queues, and message consumers read from queues.
The AMQP 0-10 Model
The AMQP 0-10 Model: Message producers write messages to exchanges, message consumers read messages from queues
A message producer creates a message, fills it with content, gives the message a routing key, and sends it to an exchange (for one kind of exchange, the fanout exchange, a routing key is optional). The routing key is simply a string that the exchange can use to determine to which queues the message should be delivered. The way the routing key is used depends on the exchange type and on the binding, which is discussed in the following paragraph. Before delivering a message, the message producer can also set various message properties in the message; for instance, one property determines whether the message is durable. A MRG Messaging broker does not lose durable messages. Even if the broker suffers a hardware failure, all durable messages are delivered when the broker is restarted. Another property can be used to specify message priority; the broker gives higher priority messages precedence.
An exchange accepts messages from message producers and routes them to message queues if the message meets the criteria expressed in a binding. A binding defines the relationship between an exchange and a message queue, specifying which messages should be routed to a given queue. If a queue is not bound to an exchange, it does not receive any messages from that exchange. The criteria found in a binding depend on the exchange type; for instance, in a direct exchange a binding might state that all messages with a given routing key should be sent to a particular queue, in a topic exchange a binding might state that a message whose routing key matches certain topics should be sent to a particular queue. This is discussed in more detail in Chapter 2, Exchanges.
A message queue holds messages and delivers them to the message consumers that subscribe to the queue. A message consumer can create, subscribe to, share, use, or destroy message queues (as long as they have permission to do so). A message queue may be durable, which means that the queue is never lost; even if the MRG Messaging Broker were to suffer a hardware failure, the queue would be restored when the broker is restarted. A message queue may be exclusive, which means only one client can consume messages from it. A message queue may also be auto-delete, which means that the queue will disappear from the server when the last client unsubscribes from the queue.
A message producer can use transactions to ensure that a group of messages are all received. In a transaction, messages and acknowledgments are batched together, and all messages in the transaction succeed or fail as a unit.