//存储未确认的消息标识tag
final SortedSet<Long> confirmSet = Collections.synchronizedNavigableSet(new TreeSet<Long>());
增加监听器
channel.addConfirmListener(new ConfirmListener() {
/*
* 消息确认返回成功
* l:如果是多条,这个就是最后一条消息的tag
* b:是否多条
* */
@Override
public void handleAck(long l, boolean b) throws IOException {
System.out.println("消息发送成功"+l+"是否多条"+b);
if(b){
confirmSet.headSet(l+1).clear();
}else{
confirmSet.remove(l);
}
} /*消息确认返回失败*/
@Override
public void handleNack(long l, boolean b) throws IOException {
System.out.println("消息发送失败"+l+"是否多条"+b);
if(b){
confirmSet.headSet(l+1).clear();
}else{
confirmSet.remove(l);
}
}
});
String routingKey ="goods.delete";
for (int i = 0; i <10; i++) {
String message = "hello ps"+i;
long tag = channel.getNextPublishSeqNo();
channel.basicPublish(Exchange_NAME,routingKey,null,message.getBytes("utf-8"));
System.out.println(tag);
confirmSet.add(tag);
}
发送者
package com.aynu.bootamqp.service;

import com.aynu.bootamqp.commons.utils.Amqp;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection; import java.io.IOException;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.TimeoutException; public class Send { private final static String Exchange_NAME ="hello";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
Connection connection = Amqp.getConnection();
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(Exchange_NAME,"topic");
//在手动确认机制之前
//一次只发送一条消息,给不同的消费者
channel.basicQos(1);
//将通道设置为comfirm模式
channel.confirmSelect(); //存储未确认的消息标识tag
final SortedSet<Long> confirmSet = Collections.synchronizedNavigableSet(new TreeSet<Long>()); channel.addConfirmListener(new ConfirmListener() { /*
* 消息确认返回成功
* l:如果是多条,这个就是最后一条消息的tag
* b:是否多条
* */
@Override
public void handleAck(long l, boolean b) throws IOException {
System.out.println("消息发送成功"+l+"是否多条"+b);
if(b){
confirmSet.headSet(l+1).clear();
}else{
confirmSet.remove(l);
}
} /*消息确认返回失败*/
@Override
public void handleNack(long l, boolean b) throws IOException {
System.out.println("消息发送失败"+l+"是否多条"+b);
if(b){
confirmSet.headSet(l+1).clear();
}else
{
confirmSet.remove(l);
}
}
});
String routingKey ="goods.delete";
for (int i = 0; i <10; i++) {
String message = "hello ps"+i;
long tag = channel.getNextPublishSeqNo();
channel.basicPublish(Exchange_NAME,routingKey,null,message.getBytes("utf-8"));
System.out.println(tag);
confirmSet.add(tag);
} channel.close();
connection.close();
}
}

接受者

package com.aynu.bootamqp.service;

import com.aynu.bootamqp.commons.utils.Amqp;
import com.rabbitmq.client.*; import java.io.IOException;
import java.util.concurrent.TimeoutException;
@SuppressWarnings("all")
public class Receive2 { private final static String QUEUE_NAME ="hello1";
private final static String Exchange_NAME ="hello";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = Amqp.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
channel.queueBind(QUEUE_NAME,Exchange_NAME,"goods.#");
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel) { @Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
super.handleDelivery(consumerTag, envelope, properties, body);
String msg = new String(body,"utf-8");
System.out.println(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
// 手动发送消息确认机制
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME,autoAck,consumer);
}
}
 

最新文章

  1. angular----关于注入HTML
  2. a标签 不触发 目标链接
  3. Json序列化与反序列化
  4. Yii源码阅读笔记(三十五)
  5. jsp页面验证码(完整实例)
  6. 腾讯优测干货精选|Android双卡双待适配——隐藏在数据库中的那些秘密
  7. 【Roman To Integer】cpp
  8. String、StringBuffer和StringBuilder——个人学习
  9. 绑定dropdownlist
  10. JS 精粹(一)
  11. hdu - 4979 - A simple math problem.(可反复覆盖DLX + 打表)
  12. 03-从零玩转JavaWeb-创建类与对象
  13. FtpUtil.java测试 (淘淘商城第三课文件上传)
  14. Spark_总结七_troubleshooting
  15. 一个使用 Web Components 的音乐播放器: MelodyPlayer
  16. sorted
  17. 应用调试(三)oops
  18. echarts功能配置实例----柱/折线、饼图
  19. zmq Poller
  20. Java基础-Java中的并法库之重入读写锁(ReentrantReadWriteLock)

热门文章

  1. 51单片机数据类型int,float,指针所占字节数
  2. centos7.4 可远程可视化桌面安装
  3. 剑指Offer 39. 平衡二叉树 (二叉树)
  4. 关于idea的debug
  5. objdump和backtrace的配合使用
  6. 一 Struts框架(上)
  7. 新系统centos7重启网络报错
  8. Spark Streaming &#39;numRecords must not be negative&#39;问题解决
  9. Nuke Python module的使用
  10. spingMVC+mybatis+spring-session共享内存配置