Seven RabbitMQ Messaging Patterns and Their Application Scenarios with Java Code Samples
This article introduces seven RabbitMQ messaging patterns—Simple, Work Queue, Publish/Subscribe, Routing, Topics, RPC, and Publisher Confirms—explains their typical use cases such as email delivery, order processing, cache synchronization, and provides complete Java code examples for each pattern.
Seven RabbitMQ Messaging Patterns Introduction and Application Scenarios
RabbitMQ acts as a message broker that forwards messages from producers to consumers. The article describes seven common messaging patterns, each with a concise explanation of when it is appropriate.
Simple (Hello World) Mode
A single producer sends messages to a single consumer via a direct queue. Application scenario: enqueue email messages for a mail service to consume.
Work Queues Mode
Multiple consumers compete for tasks from the same queue, enabling parallel processing of resource‑intensive jobs such as handling many orders simultaneously.
Publish/Subscribe Mode
A producer broadcasts messages to all bound queues using a fanout exchange. Application scenario: notify multiple caches and databases after a product inventory update.
Routing Mode
Messages are delivered to queues based on a routing key. Application scenario: send promotional messages only to consumers interested in a specific product (e.g., iPhone 12).
Topics Mode
Routing keys are matched against patterns using * (single word) and # (multiple words). Application scenario: a promotion for all iPhone models receives messages with keys like iphone12, iphone13, etc.
Remote Procedure Call (RPC) Mode
Allows a client to invoke a function on a remote server and wait for a response, useful for operations that require a result, such as order payment verification.
Publisher Confirms
Enables reliable publishing by having the broker acknowledge that messages have been persisted, essential for high‑reliability scenarios like wallet deductions.
Code Demonstration
Java examples using the RabbitMQ client library illustrate each pattern. The code shows connection setup, queue/exchange declaration, message publishing, and consumer callbacks.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Sender {
private static final String QUEUE_NAME = "simple_queue";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "simplest mode message";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("[x] Sent '" + message + "'");
channel.close();
connection.close();
}
} import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Receiver {
private static final String QUEUE_NAME = "simple_queue";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println("[x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {});
}
}Similar code blocks are provided for work queues, publish/subscribe, routing, topics, RPC, and publisher confirms, each illustrating the specific API calls (e.g., queueBind, exchangeDeclare, basicQos).
Four Exchange Types
Direct exchange: routes messages based on an exact routing key.
Fanout exchange: broadcasts to all bound queues without routing logic.
Topic exchange: matches routing keys against patterns using * and #.
Headers exchange: matches on message header attributes instead of routing keys.
Conclusion
Each messaging pattern has distinct strengths; developers should select the one that best fits their business scenario as illustrated in the examples.
References
RabbitMQ official tutorial
Official tutorial source code
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.
