(六)RabbitMQ 生成者确认(事务及Confirm机制)

publish消息确认机制

 

事务模式

如果采用标准的 AMQP 协议,则唯一能够保证消息不会丢失的方式是利用事务机制 — 令 channel 处于 transactional 模式、向其 publish 消息、执行 commit 动作。在这种方式下,事务机制会带来大量的多余开销

package com.jetsen.mq.q6transaction;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.jetsen.mq.util.ConnectionUitls;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


/**
 * rabbitmq消息确认机制之事务机制
 */
public class TxSender {

	
	private static final String QUEUE_NAME = "test_queue_tx";

	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		Connection connection = ConnectionUitls.getConnection();
		
		Channel channel = connection.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		String msgString =" send tx message";
		
		try {
			channel.txSelect();
			channel.basicPublish("", QUEUE_NAME, null, msgString.getBytes());
			System.out.println("----send" +msgString);
			channel.txCommit();
		} catch (Exception e) {
			channel.txRollback();
			System.out.println("send messaage txRollback");
		}
		
		channel.close();
		connection.close();
	}
}

 

package com.jetsen.mq.q6transaction;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.jetsen.mq.util.ConnectionUitls;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class TxRecv {

	private static final String QUEUE_NAME = "test_queue_tx";

	public static void main(String[] args) throws IOException, TimeoutException {
		Connection connection = ConnectionUitls.getConnection();
		Channel channel = connection.createChannel();
		//String queue, boolean autoAck, Consumer callback
		channel.basicConsume(QUEUE_NAME,true, new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String messageStr = new String(body,"UTF-8");
				System.out.println("Consumer receive:=>"+messageStr);
			}
		});
		
	}
}

confirm 机制

confirm 机制是在channel上使用 confirm.select方法,处于 transactional 模式的 channel 不能再被设置成 confirm 模式,反之亦然。
在 channel 被设置成 confirm 模式之后,所有被 publish 的后续消息都将被 confirm(即 ack) 或者被 nack 一次。但是没有对消息被 confirm 的快慢做任何保证,并且同一条消息不会既被 confirm 又被 nack 。
RabbitMQ 将在下面的情况中对消息进行 confirm :

  1. RabbitMQ发现当前消息无法被路由到指定的 queues 中;
  2. 非持久属性的消息到达了其所应该到达的所有 queue 中(和镜像 queue 中);
  3. 持久消息到达了其所应该到达的所有 queue 中(和镜像 queue 中),并被持久化到了磁盘(被 fsync);
  4. 持久消息从其所在的所有 queue 中被 consume 了(如果必要则会被 acknowledge)。

Confirm发送方确认模式使用和事务类似,也是通过设置Channel进行发送方确认的。

package com.jetsen.mq.q7confirm;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.jetsen.mq.util.ConnectionUitls;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class Recv {

	private static final String QUEUE_NAME = "test_queue_confirm1";

	public static void main(String[] args) throws IOException, TimeoutException {
		Connection connection = ConnectionUitls.getConnection();
		Channel channel = connection.createChannel();
		//String queue, boolean autoAck, Consumer callback
		channel.basicConsume(QUEUE_NAME,true, new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String messageStr = new String(body,"UTF-8");
				System.out.println("Consumer[test_queue_confirm1] receive:=>"+messageStr);
			}
		});
		
	}
}

Confirm的三种实现方式:

方式一:channel.waitForConfirms()普通发送方确认模式;

package com.jetsen.mq.q7confirm;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.jetsen.mq.util.ConnectionUitls;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


/**
 * rabbitmq消息确认机制之confirm
 */
public class ConfirmSender {

	
	private static final String QUEUE_NAME = "test_queue_confirm1";

	
	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
		
		Connection connection = ConnectionUitls.getConnection();
		
		Channel channel = connection.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		String msgString =" send confirm message";
		
		//生产者调用将channel设为confirm模式,注意
		channel.confirmSelect();
		channel.basicPublish("", QUEUE_NAME, null, msgString.getBytes());
		System.out.println("----send" +msgString);
		
		if(!channel.waitForConfirms()){
			System.out.println("send confirm failed");
		}else{
			System.out.println("send confirm success");
		}
		channel.close();
		connection.close();
	}
}

我们只需要在推送消息之前,channel.confirmSelect()声明开启发送方确认模式,再使用channel.waitForConfirms()等待消息被服务器确认即可。

方式二:channel.waitForConfirmsOrDie()批量确认模式;

package com.jetsen.mq.q7confirm;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.jetsen.mq.util.ConnectionUitls;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


/**
 * rabbitmq消息确认机制之confirm
 */
public class ConfirmBatchSender {

	
	private static final String QUEUE_NAME = "test_queue_confirm1";

	
	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
		
		Connection connection = ConnectionUitls.getConnection();
		
		Channel channel = connection.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		String msgString =" send confirm message";
		
		//生产者调用将channel设为confirm模式,注意
		channel.confirmSelect();
		
		for (int i = 0; i < 10; i++) {
			channel.basicPublish("", QUEUE_NAME, null, msgString.getBytes());
		}
		
//		if(!channel.waitForConfirms()){
//			System.out.println("send confirm failed");
//		}else{
//			System.out.println("send confirm success");
//		}
		channel.waitForConfirmsOrDie(); //直到所有信息都发布,只要有一个未确认就会IOException
		channel.close();
		connection.close();
	}
}

以上代码可以看出来channel.waitForConfirmsOrDie(),使用同步方式等所有的消息发送之后才会执行后面代码,只要有一个消息未被确认就会抛出IOException异常。

方式三:channel.addConfirmListener()异步监听发送方确认模式;

package com.jetsen.mq.q7confirm;

import java.io.IOException;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.TimeoutException;

import com.jetsen.mq.util.ConnectionUitls;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;

/**
 * 异步模式
 * Channel 对象提供的ConfirmListener()回调方法之包含deliveryTag(当前Channel发出
 * 的消息序号),我们需要自己为每一个Channel维护一个unconfirm的消息序号集合,每publish
 * 一条数据,集合中元素就+1,每回调一次handlerAck方法,unconfirm结合删掉相应的一条(multiple=false)
 * 或者多条(multiple=true)记录,从程序运行效率上看,这个unconfirm集合最好采用有序集合
 * SortedSet存储结构
 * @author jetsen
 *
 */
public class ConfirmAysnSender {
	
	
	private static final String QUEUE_NAME = "test_queue_confirm3";

	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		Connection connection = ConnectionUitls.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<Long>());
		
		channel.addConfirmListener(new ConfirmListener() {
			
			@Override
			public void handleNack(long deliveryTag, boolean multiple) throws IOException {
				if(multiple){
					System.out.println("----handlerNack-----multiple");
					confirmSet.headSet(deliveryTag+1).clear();
				}else{
					System.out.println("----handlerNack-----multiple----false");
					confirmSet.remove(deliveryTag);
				}
			}
			
			@Override
			public void handleAck(long deliveryTag, boolean multiple) throws IOException {
				if(multiple){
					System.out.println("----handleAck-----multiple");
					confirmSet.headSet(deliveryTag+1).clear();
				}else{
					System.out.println("----handleAck-----multiple----false");
					confirmSet.remove(deliveryTag);
				}
			}
		});
		
		String msgString =" send confirm Aysn message";
		while(true){
			long segNo = channel.getNextPublishSeqNo();
			channel.basicPublish("", QUEUE_NAME, null, msgString.getBytes());
			confirmSet.add(segNo);
			
		}
	}
}

异步模式的优点,就是执行效率高,不需要等待消息执行完,只需要监听消息即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值