RabbitMq confirmation mechanism


1. RabbitMQ feature

    persistence: To persist messages to disk, three conditions need to be met:
  • The delivery mode option is set to 2 (persistent)
  • send to persistent exchange
  • Arrived on persistent queue

    Things: RabbitMQ supports things, but things are synchronous and the performance is too poor. There are three methods related to the transaction mechanism in RabbitMQ: txSelect(), txCommit() and txRollback(), txSelect is used to set the current channel to transaction mode, txCommit is used to commit transactions, and txRollback is used to roll back transactions. After opening the transaction, we can publish the message to the broker proxy server. If the txCommit submission is successful, the message must have reached the broker. If the broker crashes abnormally or throws an exception for other reasons before txCommit is executed, then we can Catch the exception and roll back the transaction through txRollback.
try {
    channel.txSelect();
    channel.basicPublish(exchange, routingKey, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
    channel.txCommit();
} catch (Exception e) {
    e.printStackTrace ();
    channel.txRollback();
}


    Confirmation mechanism (Confirm): It is divided into consumer confirm and producer confirm. Both transactions and acknowledgement mechanisms are designed to ensure that messages are not lost. The acknowledgement mechanism is asynchronous and is more performant than transactions.
  • basic.ack: acknowledge receipt of the message
  • basic.nack: reject message, support requeue, support batch nack (enhanced version of reject)
  • basic.reject: reject message, support requeue, do not support batch

    The broker sends messages to the consumer in turn. If the message of the requeue is sent to the next consumer in turn. If there is no Ack or NoAck, the broker will suspend the message (the message will not be requeued until the Consumer that consumes the message is disconnected), and the Consumer without NoAck will still receive the message.

2. If

  autoAck is set to true when Consumer Confirm subscribes to a message, then you do not need to send a confirmation message back to the server after processing the message. This can greatly speed up the speed of consumer consumption. Otherwise, the code needs to be manually acked.
final Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body)
                throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
            this.getChannel().basicAck(envelope.getDeliveryTag(), false);
        }
};
channel.basicConsume(QUEUE_NAME, false, consumer); //The second parameter autoAck is false


3. Producer Confirm The

    producer sets the channel to confirm mode. Once the channel enters the confirm mode, all messages published on the channel will be assigned a unique ID (starting from 1). Once the message is delivered to all matching queues , the broker will send an acknowledgment to the producer (containing the unique ID of the message), which allows the producer to know that the message has arrived at the destination queue correctly. If the message and the queue are durable, the acknowledgment message will write the message to the After the disk is sent out, the deliver-tag field in the acknowledgment message returned by the broker to the producer contains the serial number of the acknowledgment message. In addition, the broker can also set the multiple field of basic.ack to indicate that all messages before this serial number have been received. processed.

    The biggest advantage of the confirm mode is that it is asynchronous. Once a message is published, the producer application can continue to send the next message while waiting for the channel to return confirmation. When the message is finally confirmed, the producer application can pass the callback method. To process the confirmation message, if RabbitMQ loses the message due to its own internal error, a nack message will be sent, and the producer application can also process the nack message in the callback method.

    After the channel is set to confirm mode, all subsequent messages published will be confirmed (ie ack) or nacked once. But there is no guarantee about how quickly a message is confirmed, and the same message cannot be both confirmed and nacked.
  
    A channel already in transaction mode cannot be set to confirm mode, that is, these two modes cannot coexist.
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.confirmSelect(); //Producer side open confirm mode
        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();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326298333&siteId=291194637
Recommended