Detailed explanation of the message confirmation mechanism of the Rabbitmq framework in the Java library
Rabbitmq is an open source message middleware that is widely used in the construction of highly reliable and highly scalability distributed systems.When using RabbitMQ, the message confirmation mechanism is an important part of ensuring the reliability of message transmission.This article will introduce the message confirmation mechanism of the Rabbitmq framework in the Java class library and provide the corresponding Java code example.
The message confirmation mechanism means that after the producer sent a message to Rabbitmq, it is ensured that the message has been correctly accepted and processed.Rabbitmq provides two types of message confirmation mechanisms: transaction -based message confirmation and message confirmation based on publisher confirmation.
1. Affairs of transaction -based information confirmation
When using transaction -based messages to confirm, start a transaction through Rabbitmq. After the producer sends messages, it is necessary to sign up the transaction to confirm the reception of the message.If the transaction is successfully submitted, it means that the message has been successfully sent to Rabbitmq, otherwise the transaction will be returned and the news will be re -sent.
The following is a sample code confirmed by transaction -based messages:
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
try {
channel.txSelect();
// Send a message
channel.basicPublish(exchange, routingKey, null, message.getBytes());
// Submit a transaction
channel.txCommit();
} catch (IOException e) {
// Affairs rollback
channel.txRollback();
} finally {
channel.close();
connection.close();
}
2. Frequently confirmed by the message confirmed by the publisher
The message confirmed based on the publisher was achieved by the `Confirm` mechanism provided by Rabbitmq.When the producer sends a message, it can wait for the confirmation of Rabbitmq by calling the `WaitForconfirms` method to ensure that the message has been correctly received and processed.
The following is a sample code confirmed by a message confirmed by the publisher:
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// Open the publisher confirmation mode
channel.confirmSelect();
// Add a monitor
channel.addConfirmListener(
(sequenceNumber, multiple) -> {
if (multiple) {
System.out.println ("The message has been confirmed, the number:" + Sequeencenumber);
} else {
System.out.println ("The message has been confirmed, the number:" + Sequeencenumber);
}
},
(sequenceNumber, multiple) -> {
if (multiple) {
System.out.println ("The message is not confirmed, the number:" + sequeencenumber);
} else {
System.out.println ("The message is not confirmed, the number:" + sequeencenumber);
}
}
);
// Send a message
channel.basicPublish(exchange, routingKey, null, message.getBytes());
// Waiting for confirmation
channel.waitForConfirmsOrDie();
channel.close();
connection.close();
In the above code, a `ConfirmListener` listener is added to process the confirmation and unsolved information of the message.When the message is successfully sent to Rabbitmq, the `handleack` method will be called to process the logic of the confirmation of the message; when the message fails to successfully send it, the` handlenack` method will be called and process the logic that the message is not confirmed.
Summarize:
Through this article, we introduced in detail the message confirmation mechanism of the Rabbitmq framework in the Java library.Affairs -based message confirmation needs to be significantly submitted or rolled back to ensure the receiving of the message; the message confirmation based on the publisher confirmation is to ensure the reliable transmission of the message by waiting for the confirmation of Rabbitmq.According to actual needs and scenes, choosing a suitable message confirmation mechanism is essential for building a reliable distributed system.