RabbitMQ Basics: Concepts, Working Modes, and Java Code Samples
This article introduces RabbitMQ's core concepts, compares AMQP with JMS, outlines its key features, and demonstrates four working patterns—work queue, routing, publish/subscribe, and wildcard—through detailed Java producer and consumer code examples.
This article explains the fundamental concepts of RabbitMQ, including the distinction between AMQP (a protocol) and JMS (a Java API), and highlights the main characteristics of message queues such as decoupling, asynchronous acceleration, and load smoothing.
It then describes four typical messaging patterns and provides complete Java code for both producers and consumers.
1. Work Queue Mode
In this pattern a single producer sends messages to a single queue, while multiple consumers compete for those messages. The consumer receives messages in no particular order.
for (int i = 0; i < 10; i++) {
String body = i + "hello rabbit";
channel.basicPublish("", "work_queue", null, body.getBytes());
}2. Routing (Direct) Mode
Messages are routed to queues based on a routing key; both the exchange type (DIRECT) and the routing key must match on producer and consumer sides.
// Producer example for direct exchange
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("host");
factory.setPort(5672);
factory.setVirtualHost("/vhost");
factory.setUsername("user");
factory.setPassword("pass");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
String exchangeName = "direct";
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, false, null);
channel.queueDeclare("routing_queue1", true, false, false, null);
channel.queueDeclare("routing_queue2", true, false, false, null);
channel.queueBind("routing_queue1", exchangeName, "error");
channel.queueBind("routing_queue2", exchangeName, "info");
String body = "日志信息-info";
channel.basicPublish(exchangeName, "info", null, body.getBytes());
channel.basicPublish(exchangeName, "error", null, body.getBytes());
channel.close();
connection.close();3. Publish/Subscribe (Fanout) Mode
All messages sent to a fanout exchange are broadcast to every bound queue, ignoring routing keys.
// Producer example for fanout exchange
String exchangeName = "fanout";
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT, true, false, false, null);
channel.queueDeclare("queue1", true, false, false, null);
channel.queueDeclare("queue2", true, false, false, null);
channel.queueBind("queue1", exchangeName, "");
channel.queueBind("queue2", exchangeName, "");4. Wildcard (Topic) Mode
Using a topic exchange, routing keys can contain wildcards (e.g., "#.error" or "order.*") to flexibly direct messages to matching queues.
channel.basicPublish(exchangeName, "goods.error", null, body.getBytes());For each pattern, the article also provides the corresponding consumer code, which creates a connection factory, sets host/port/credentials, opens a channel, declares the queue, and consumes messages using a DefaultConsumer implementation.
// Consumer skeleton
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("host");
factory.setPort(5672);
factory.setVirtualHost("/vhost");
factory.setUsername("user");
factory.setPassword("pass");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("hello_world", true, false, false, null);
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body));
super.handleDelivery(consumerTag, envelope, properties, body);
}
};
channel.basicConsume("hello_world", true, consumer);Overall, the article serves as a concise guide for developers to understand RabbitMQ's architecture and quickly implement common messaging scenarios in Java.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.