AMQP is a message protocol that deals with publishers and consumers. It would look a lot like JMS but it is not. The main entities are Exchanges, Queues and Bindings. Look at the following diagram
So a producer would send a message to the exchange and it is the job of the message broker (RabbitMQ in our case) to ensure that the messages are delivered to the right queue.
But first the connections have to be built so that publishers can publish on the exchange and consumers can listen from it. Let us see how this is done in Akka in our project. Our framework gets initialized by the following method
I have taken out the business logic and left out the methods which would form the core of this post. Let us go method by method, first, we get all the connection information which would be relevant for us to make the connection
Here, use the Scala XML magic and get done soon. Our sample XML through which
Now let us see how we get a connection
As you would notice, all the connection parameters are passed to the akka.amqp.AMQP.ConnectionParameters and a connection object is received.
Once we have the connection, let us instantiate the producer. The method instantiateProducer(connection, exchangeParameters) looks like this
As you would notice, we pass exchangeParameters to the producer. You would see in the diagram above that AMQP producer requires a reference of the exchange to which it can send messages. We get the exchange information as follows
If in our configuration we define the exchange to be durable, then we set the durable and autodelete properties of the exchange.
This would set up our producer actor and we would be able to send messages to the producer like this with the bang operator
producer ! Message(message)
As soon as the producer actor would receive the message, it would put it on the exchange that we have specified. Now, let us look at the consumer
Hence, now as shown in the figure above, we have a consumer listening on the queue called myqueue which is listening to the exchange myexchange. You would notice that we have a “@vajrafeed” This is the binding key which lets the messgage broker decide, which messages it should pass on onto the consumer. If there was another consumer attached to the queue with a binding key say @nyse then the messages for @vajrafeed would not be delivered to it. You can get more information about AMQP here.