1.概述
发布者确认 是实现可靠发布的RabbitMQ扩展。在通道上启用发布者确认后,代理将异步确认客户端发布的消息,这意味着它们已在服务器端处理。
发布者确认是AMQP 0.9.1协议的RabbitMQ扩展,因此默认情况下未启用它们。
1.1 confirm模式的实现原理:
生产者将信道设置成confirm模式,一旦信道进入confirm模式,所有在该信道上面发布的消息都会被指派要给唯一的ID(从1开始),一旦消息被投递到所有匹配的队列后,broker就会发送一个确认给生产者(包含消息的唯一ID),这就使得生产者知道消息已经正确到达目的队列了,如果消息和队列是可持久化的,那么确认消息会将消息写入磁盘之后发出,broker回传给生产者的确认消息中deliver-tag域包含了确认消息的序列号,此外broker也可以设置basic.ack的multiple域。表示到这个序列号之前的所有消息都已经得到了处理。
1.2 启用发布者确认
Channel channel = connection.createChannel();
channel.confirmSelect();
1.3 编程模式(策略):
策略1:单条发布消息
策略2:批量发布消息
策略3:处理发布者异步确认
2.代码示例
2.1 单条发布消息
public class ConfirmSender1 {
private final static String queue_name = "test_queue_confirm1";
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queue_name, false, false, false, null);
/**生产者调用confirmSelect将channel设置为confirm模式*/
channel.confirmSelect();
String msg = "hello confirm1 msg!";
channel.basicPublish("", queue_name, null, msg.getBytes());
if(channel.waitForConfirms()){
System.out.println("msg send ok!");
}else{
System.out.println("msg send fail!");
}
channel.close();
connection.close();
}
}
2.2 批量发布消息
public class ConfirmSender2 {
private final static String queue_name = "test_queue_confirm2";
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queue_name, false, false, false, null);
/**生产者调用confirmSelect将channel设置为confirm模式*/
channel.confirmSelect();
String msg = "hello confirm2 msg!";
//批量
for (int i = 0; i < 100; i++) {
// Thread.sleep(300);
channel.basicPublish("", queue_name, null, msg.getBytes());
}
if(channel.waitForConfirms()){
System.out.println("msg send ok!");
}else{
System.out.println("msg send fail!");
}
channel.close();
connection.close();
}
}
2.3 异步确认
channel对象提供的confirmListenner()回调方法只包含了deliveryTag(当前channel发出的消息序号),我们需要自己为每一个channel维护一个unconfirm的消息序号集合,每publish一条数据,集合中元素加1,每回调一次handleAck方法,unconfirm集合删掉相应的一条(multiple=false)或者多条(multiple=true)记录。从程序运行效率上看,这个unconfirm集合最好采用有序集合SortedSet存储结构。
代码
public class ConfirmSender3 {
private final static String queue_name = "test_queue_confirm3";
public static void main(String[] args) throws Exception {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(queue_name, false, false, false, null);
/**生产者调用confirmSelect将channel设置为confirm模式*/
channel.confirmSelect();
//存放未确认的消息标识
final SortedSet<Long> confirmSet = Collections.synchronizedSortedSet(new TreeSet<>());
//通道添加监听
channel.addConfirmListener(new ConfirmListener() {
/**正常回执*/
@Override
public void handleAck(long deliveryTag, boolean multiple) throws IOException {
if(multiple){
System.out.println("====handleAck==== multiple is true == headSet");
confirmSet.headSet(deliveryTag+1).clear();
}else{
System.out.println("====handleAck==== multiple is false == remove");
confirmSet.remove(deliveryTag);
}
}
/**回执异常,再作业务处理*/
@Override
public void handleNack(long deliveryTag, boolean multiple) throws IOException {
if(multiple){
System.out.println("====handleNack==== multiple is true == headSet");
confirmSet.headSet(deliveryTag+1).clear();
}else{
System.out.println("====handleNack==== multiple is false == remove");
confirmSet.remove(deliveryTag);
}
}
});
String msg = "hello confirm3 msg!";
while (true){
long segNo = channel.getNextPublishSeqNo();
channel.basicPublish("",queue_name,null,msg.getBytes());
confirmSet.add(segNo);
}
}
}