RabbitMQ Hello World Tutorial Using the Java Client
This article introduces RabbitMQ as a message broker, explains producer‑consumer terminology, and provides a step‑by‑step Java "Hello World" example with full code, compilation, and execution instructions for both sender and receiver programs.
RabbitMQ is a message broker that receives and forwards messages, similar to a post office that stores and delivers parcels. Unlike a traditional post office, RabbitMQ stores messages in binary form without handling files.
In RabbitMQ terminology, a producer (P) sends messages, a queue stores them, and a consumer (C) receives them. Producers, consumers, and the broker can run on different machines.
The article then presents a "Hello World" example using the RabbitMQ Java client, which follows the AMQP protocol. After downloading and extracting the client JAR files, the tutorial shows how to write two Java programs: Send.java (producer) and Recv.java (consumer).
$ unzip rabbitmq-java-client-bin-*.zip
$ cp rabbitmq-java-client-bin-*/*.jar ./Send.java imports the necessary classes, creates a connection to localhost, declares a queue named hello, publishes the message "Hello World!", prints a confirmation, and finally closes the channel and connection.
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class Send {
private static final String QUEUE_NAME = "hello";
public static void main(String[] argv) throws java.io.IOException {
// ... create factory, connection, channel ...
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
}If the message is not printed, the article suggests checking the broker’s log for issues such as insufficient disk_free_limit space.
Recv.java also imports the client classes, creates a connection and channel, declares the same queue, and uses QueueingConsumer to wait for messages. It continuously receives deliveries, converts the body to a string, and prints each received message.
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class Recv {
private static final String QUEUE_NAME = "hello";
public static void main(String[] argv) throws java.io.IOException, java.lang.InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
}
}
}Both programs are compiled with the client JAR on the classpath:
$ javac -cp rabbitmq-client.jar Send.java Recv.javaThey are then run (on Unix‑like systems) with:
$ java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send
$ java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar RecvOn Windows, the classpath separator is a semicolon ( ;). The receiver keeps running, waiting for messages until interrupted with Ctrl‑C. The article also mentions how to list queues using rabbitmqctl list_queues and provides a brief introduction to building a simple work queue.
Hello World!
Original source: my.oschina.net/OpenSourceBO/blog/379732
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
