rabbitmq message acknowledgment mechanism: + confirm the transaction

package com.example.demo.util;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.SortedSet;
import java.util.TreeSet;


/**
* rabbitmq 工具类
*
* @author yangxj
* @date 2020-03-25 20:30
*/
public class RabbitMQUtils {
/**
* 获取rabbitMq 连接
*
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername ( "ADMIN");
factory.setUsername ( "ADMIN");
return factory.newConnection ();
}

/ **
* 1. by transaction mechanisms (disadvantages: the decreases throughput mq)
* /

public void useTx () throws Exception {
Connection Connection = the getConnection ();

Channel Channel connection.createChannel = ();

// declare a queue
channel.queueDeclare ( "Test-queue", to false, to false, to false, null);

String Message = "Hello MQ Rabbit! ";

channel.txSelect (); // open transaction

the try {
channel.basicPublish (" "," Test-Queue ", null, message.getBytes ()); // send message
channel.confirmSelect ();// submit
The catch} (Exception E) {
channel.txRollback (); // Rollback
} {the finally
channel.close ();
channel.close ();
}

}

/ **
* 2. Manufacturer confirm mode (serial type)
* Principle : when the channel channel enters confrim mode, all messages posted in the channel will be assigned a unique ID
* when the message is delivered to all matching queue, Broker will send an acknowledgment to the producer (including message ID);
* /
public void producterConfirmSync () throws Exception {
connection connection = the getConnection (); // Get rabbitMq connected

Channel Channel connection.createChannel = ();

channel.confirmSelect (); // open mode confirm

String message = "hello rabbit mq! " ;
String message2 = "The Rabbit MQ2 the Hello!";

channel.basicPublish("", "", MessageProperties.BASIC, message.getBytes());
channel.basicPublish("", "", MessageProperties.BASIC, message2.getBytes());

if (channel.waitForConfirms()) { // 支持多条发送后再确认
System.out.println("message send success..");
} else {
System.out.println("message send fail..");
}
}

/**
* 3. 生产者 confirm模式(异步)
*/
public void producterConfirmAsync() throws Exception {
Connection connection = getConnection(); // 获取rabbitMq 连接

Channel channel = connection.createChannel();

channel.confirmSelect();// open confirm mode

// ack ids
The SortedSet <Long> ackIds = new new TreeSet <> ();

// Not ACK IDS
the SortedSet <Long> nackIds = new new TreeSet <> ();

for (int I = 0; I <100; I ++) {
String Message = "Hello MQ Rabbit: "+ I;
channel.basicPublish (" "," ", null, message.getBytes ());
}


channel.addConfirmListener (new new ConfirmListener () {
@Override
public void handleAck (Long deliveryTag, Multiple Boolean) throws IOException {// sent successfully (acknowledgment)
// multiple single or multiple confirmation acknowledgment (true, indicating that all previous messages have the serial number has been processed)
ackIds.add (deliveryTag);
}

@Override
public void handleNack (long deliveryTag, boolean multiple) throws IOException {// transmission failed (unacknowledged)
nackIds.add (deliveryTag);
}
});
}

/ **
* consumer Confirm
* /
public void consumerConfirm () throws Exception {
connection connection = getConnection (); // Get rabbitMq connected

Channel Channel connection.createChannel = ();

the try {
channel.basicConsume ( "Test", to false, // automatically turn off the consumer ACK
new new DefaultConsumer (Channel) {
@Override
public void handleDelivery (String consumerTag,
Envelope Envelope,
AMQP.BasicProperties Properties,
byte [] body)
throws IOException {
// release of each message will receive a unique deliveryTag, deliveryTag channel is unique within the scope of
Long deliveryTag envelope.getDeliveryTag = ();
// first two parameters are the bulk acknowledgment flag. If true, the bulk acknowledgment is executed, the received message to confirm before all deliveryTag;
// if the value is false, only the currently received message to confirm
channel.basicAck (deliveryTag, true); // Confirm
}
});
} catch (IOException e) {
e.printStackTrace();
}finally {
channel.close();
connection.close();
}
}



}

Guess you like

Origin www.cnblogs.com/yangxijun/p/12570828.html
Recommended