MRG Messaging implements the AMQP specification, which was written to create an open standard for interoperable messaging. AMQP defines both a wire level protocol (the transport layer) and higher level semantics for messaging (the functional layer). It is completely free to use and is being developed by the AMQP Working Group. AMQP is currently in draft and will be submitted to a standards body once it is completed.
The following diagram shows how the MRG Messaging broker is used by producer-consumer applications. Message producers write to exchanges, exchanges route messages to queues, and message consumers read from queues.
The AMQP Model: Message producers write messages to exchanges, message consumers read messages from queues
Now let's look at this in more detail. A message producer creates a message, fills it with content, gives the message a routing key (for one kind of exchange, the fanout exchange, a routing key is optional), and sends it to an exchange. 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 is discussed later in this chapter). 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. MRG Messaging guarantees that a broker does not lose durable messages; even if the MRG Messaging Broker were to suffer a hardware failure, all durable messages would be delivered when the broker is restarted. Another property can be used to specify message priority; the broker gives higher priority messages precedence over lower priority messages.
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. For instance, a binding might state that all messages with a given routing key should be sent to a particular queue. If a queue is not bound to an exchange, it does not receive any messages from that exchange.
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 when the broker is restarted. A message queue may be exclusive, which means only one client can use 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 acknowledgements acknowledgements are batched together, and all messages in the transaction succeed or fail as a unit.