RabbitMQ之confirm异步模式

RabbitMQ之confirm异步模式简介

 * Channel 对象提供的ConfirmListener()回调方法只包含
 * deliveryTag(当前Chanel发出的消息序号),我们需要自己
 * 为每一个Channel维护一个unconfirm的消息序号集合,每publish
 * 一条数据,集合中元素加1,每回调一次handleAck方法,unconfirm集合
 * 删除相应的一条(multiple=false)或多条(multiple=true)记录,从
 * 程序运行效率上看,这个unconfirm集合最好采用有序集合
 * SortedSet存储结构

 

代码参考

生产者

/**
 * RabbitMQ 之confirm异步
 * 
 * Channel 对象提供的ConfirmListener()回调方法只包含 deliveryTag(当前Chanel发出的消息序号),我们需要自己
 * 为每一个Channel维护一个unconfirm的消息序号集合,每publish
 * 一条数据,集合中元素加1,每回调一次handleAck方法,unconfirm集合
 * 删除相应的一条(multiple=false)或多条(multiple=true)记录,从 程序运行效率上看,这个unconfirm集合最好采用有序集合
 * SortedSet存储结构
 * 
 * @author zhang
 *
 */
public class ConfirmAsySend {

	public static final String QUEUE_NAME = "asy_test_confirm";

	public static void main(String[] args) throws IOException, TimeoutException {

		// 获取连接
		Connection connection = ConnectHelp.getConnect();

		// 建立通道
		Channel channel = connection.createChannel();

		// 声明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);

		// 开启事务
		channel.confirmSelect();

		// 创建set
		final SortedSet<Long> confirmSet = Collections.synchronizedSortedSet(new TreeSet<Long>());

		// 监听事务
		channel.addConfirmListener(new ConfirmListener() {

			// handleNack
			public void handleNack(long arg0, boolean arg1) throws IOException {
				// TODO Auto-generated method stub

				if (arg1) {
					System.out.println("handleNack : " + arg1);
					confirmSet.headSet(arg0 + 1).clear();
				} else {
					System.out.println("handleNack : " + arg1);
					confirmSet.remove(arg0);
				}
			}

			// 没有问题
			public void handleAck(long arg0, boolean arg1) throws IOException {
				// TODO Auto-generated method stub

				if (arg1) {
					System.out.println("handleAck :  " + arg1);
					confirmSet.headSet(arg0 + 1).clear();
				} else {
					System.out.println("handleAck :  " + arg1);
					confirmSet.remove(arg0);
				}

			}
		});

		// 发送的数据
		String msg = "msg data";

		while (true) {
			long seqNo = channel.getNextPublishSeqNo();
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
			confirmSet.add(seqNo);
		}

	}

}

 

消费者

/**
 * RabbitMQ 之comfirm 异步 消费者
 * 
 * @author zhang
 *
 */
public class ConfirmAsyRevc {

	public static final String QUEUE_NAME = "asy_test_confirm";

	public static void main(String[] args) throws IOException, TimeoutException {

		// 获取连接
		Connection connetion = ConnectHelp.getConnect();

		// 创建通道
		Channel channel = connetion.createChannel();

		// 声明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);

		// 接受数据,并监听
		channel.basicConsume(QUEUE_NAME, new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {

				System.out.println("confirm Revc:" + new String(body, "utf-8"));

			}
		});

	}

}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值