Every application that uses AMQP needs to establish a connection with the AMQP broker. By default, RabbitMQ (as well as any other AMQP broker up to version 1.0) works over TCP as a reliable transport protocol on port 5672, that is, the IANA-assigned port.
We are now going to discuss how to create the connection. In all the subsequent recipes we will refer to the connection and channel as the results of the operations presented here.
Getting ready
To use this recipe we need to set up the Java development environment as mentioned in the Introduction section.
How to do it…
In order to create a Java client that connects to the RabbitMQ broker, you need to perform the following steps:
Import the needed classes from the Java RabbitMQ client library in the program namespace:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
Create an instance of the client ConnectionFactory:
ConnectionFactory factory = new ConnectionFactory();
Set the ConnectionFactory options:
factory.setHost(rabbitMQhostname);
Connect to the RabbitMQ broker:
Connection connection = factory.newConnection();
Create a channel from the freshly created connection:
Channel channel = connection.createChannel();
As soon as we are done with RabbitMQ, release the channel and the connection:
In this recipe we are learning how to send a message to an AMQP queue. We will be introduced to the building blocks of AMQP messaging: messages, queues, and exchanges.
You can find the source at Chapter01/Recipe02/src/rmqexample.
Getting ready
To use this recipe we need to set up the Java development environment as indicated in the Introduction section.
How to do it…
After connecting to the broker, as seen in the previous recipe, you can start sending messages performing the following steps:
Declare the queue, calling the queueDeclare() method on com.rabbitmq.client.Channel:
In this recipe we are closing the loop; we have already seen how to send messages to RabbitMQ—or to any AMQP broker—and now we are ready to learn how to retrieve them.
You can find the source code of the recipe at Chapter01/Recipe03/src/rmqexample/nonblocking.
Getting ready
To use this recipe we need to set up the Java development environment as indicated in the introduction.
How to do it…
In order to consume the messages sent as seen in the previous recipe, perform the following steps:
Declare the queue where we want to consume the messages from:
In AMQP the messages are opaque entities; AMQP does not provide any standard way to encode/decode them.
However, web applications very often use JSON as an application layer format, that is, the JavaScript serialization format that has become a de-facto standard; in this way, the RabbitMQ client Java library can include some utility functions for this task.
On the other side, this is not the only protocol; any application can choose its own protocol (XML, Google Protocol Buffers, ASN.1, or proprietary).
In this example we are showing how to use the JSON protocol to encode and decode message bodies. We are using a publisher written in Java (Chapter01/Recipe04/Java_4/src/rmqexample) and a consumer in Python (Chapter01/Recipe04/Python04).
Getting ready
To use this recipe you will need to set up Java and Python environments as described in the introduction.
How to do it…
To implement a Java producer and a Python consumer, you can perform the following steps:
Java: In addition to the standard import described in the recipe Connecting to the broker, we have to import: