Java developer must read: In -depth understanding of the news release and subscription mode of Rabbitmq
Java developer must read: In -depth understanding of the news release and subscription mode of Rabbitmq
Rabbitmq is an open source message middleware that is widely used in asynchronous communication, event -driven architecture and microservices architecture.In the message queue, message release and subscription mode are one of the most commonly used models. It allows multiple consumers to receive messages at the same time to achieve decoupled, extended and parallel processing.
This article will explore the principles and usage methods of Rabbitmq messages and subscription mode, and explain the specific Java code examples.
1. The principle of news release and subscription mode
In the message release and subscription mode, the publisher of the message sends the message to Exchange, and the switch is responsible for the message route to one or more queue.Consumer receives messages by subscribing queue.
Specifically, the message release and subscription mode contains the following main components:
1. Message Publisher (Producer): Responsible for generating messages and sending to switches.
2. Exchange (Exchange): Receive messages from the message publisher, and ride the message to one or more queues in accordance with certain rules.
3. Queue: The container of the storage message, multiple consumers can subscribe to the same queue to receive the message.
4. Consume: Subscribe to the queue and receive messages in the queue.
How to use the RabbitMQ message release and subscription mode
1. Create connection and channels
First, we need to use the Java code to create a RabbitMQ connection and channel to communicate with the RabbitMQ server.The following is a simple Java code example:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
2. Create a switch and queue
Next, we need to create switches and queues and bind them.Here are examples of Java code for creating switches and queues:
String exchangeName = "myExchange";
String queueName = "myQueue";
channel.exchangeDeclare(exchangeName, "fanout");
channel.queueDeclare(queueName, false, false, false, null);
channel.queueBind(queueName, exchangeName, "");
In the above code, we use the FANOUT type switch (ie, the broadcast mode), which means that the switch will make the message routing all the binding queues.
3. Post message
Now, we can send the message to the switch with a message publisher.The following is an example of Java code that sends a message:
String message = "Hello, RabbitMQ!";
channel.basicPublish(exchangeName, "", null, message.getBytes("UTF-8"));
4. Subscribe to message
Finally, we can create a consumer to subscribe to the queue and receive messages in the queue.The following is an example of a Java code for subscribing messages:
boolean autoAck = true;
channel.basicConsume(queueName, autoAck, (consumerTag, delivery) -> {
String receivedMessage = new String(delivery.getBody(), "UTF-8");
System.out.println("Received message: " + receivedMessage);
}, consumerTag -> {});
In the above code, we use Lambda expressions to define the logic of consumers receiving messages.
3. Summary
By in -depth understanding and subscription mode of Rabbitmq, we can better use the message queue in Java development to achieve asynchronous communications and dynamic coupling.This article provides an interpretation of the principles and methods of information publishing and subscription mode, and comes with corresponding Java code examples, hoping to help Java developers.