1.首先是发布消息者的代码

public class MQPublisher {public static void main(String[] args) {try{ConnectionFactory factory = new ConnectionFactory();factory.setHost("123.199.200.54");//129.199.200.54factory.setUsername("omsuser");factory.setPassword("9aVXR2LLk2xskcDq7ziccWKP");factory.setPort(5672);Connection connection = factory.newConnection();Channel channel = connection.createChannel();channel.queueDeclare("ManufactureBill", true, false, false, null);String bill = getManufactureBill();MQMessage mqmsg=new MQMessage();JSONObject jso=JSON.parseObject(bill);//json字符串转换成jsonobject对象mqmsg.setObject(jso);mqmsg.setType(0);String MQjson = JSON.toJSONString(mqmsg,SerializerFeature.WriteMapNullValue);//channel.basicPublish("", "ManufactureBill", null, bill.getBytes());// channel.queueDelete("ManufactureBill");channel.basicPublish("", "ManufactureBill", null, MQjson.getBytes());channel.close();connection.close();}catch(Exception e){e.printStackTrace();}}

定义通道

AMQP.Queue.DeclareOk queueDeclare(String queue,boolean durable,boolean exclusive,boolean autoDelete,Map<String,Object> arguments)throws IOException
Declare a queue
Parameters:
queue - the name of the queue 通道的名称,消费者要接收信息,名称要与此相同,同一个连接下面会可能有多个通道
durable - true if we are declaring a durable queue (the queue will survive a server restart)  定义一个持久的队列,一般设置为true,当服务器挂掉之后,队列依然存在,
exclusive - true if we are declaring an exclusive queue (restricted to this connection)   定义一个独有队列(不允许别人连接)一般设置为false,不然怎么接受消息
autoDelete - true if we are declaring an autodelete queue (server will delete it when no longer in use)  当服务器不在用到此队列的时候,是否自动删除。一般设置为false,和durable一致
arguments - other properties (construction arguments) for the queue  这个不知道
Returns:
a declaration-confirm method to indicate the queue was successfully declared
Throws:
IOException - if an error is encountered
See Also:
AMQP.Queue.Declare, AMQP.Queue.DeclareOk

2. 消费者:通过监听器方式,项目启动便启动,开启一个线程执行下面代码,while(true)无限循环,间隔一定时间。然后处理消息(nacked确认),不发送还是,重新发送,如果不确认,消息无法被重新投递

 boolean state=true;Channel channel=null;QueueingConsumer.Delivery delivery=null;try {ConnectionFactory factory = new ConnectionFactory();           factory.setHost("134.129.200.54");System.out.println("************收到ykl订单信息**************************");factory.setUsername("omsuser");factory.setPassword("9aVXR2LLk2xs54Dq7ziccWKP"); //9aVXR2LLk2xskcDq7ziccWKP//123456factory.setPort(5672);Connection connection = factory.newConnection();channel = connection.createChannel();channel.queueDeclare("ManufactureBill", true, false, false, null);QueueingConsumer consumer = new QueueingConsumer(channel);// channel.basicConsume("ManufactureBill", false, consumer);           delivery= consumer.nextDelivery();String message = new String(delivery.getBody(),"utf-8"); System.out.println("接收数据程序加载完成");channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);  //不需要再次发送   } catch (Exception e) {try {channel.basicNack(delivery.getEnvelope().getDeliveryTag(),true, false);//再次发送} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}

void basicReject(long deliveryTag, boolean requeue) throws IOException;

第一个参数指定 delivery tag,相当于这条消息的id,第二个参数说明如何处理这个失败消息。requeue 值为 true 表示该消息重新放回队列头,值为 false 表示放弃这条消息。

一般来说,如果是系统无法处理的异常,我们一般是将 requeue 设为 false,例如消息格式错误,再处理多少次也是异常。调用第三方接口超时这类异常 requeue 应该设为 true。

从 basicReject 方法参数可见,取消确认不支持批量操作(类似于 basicAck 的 multiple 参数)。所以,RabbitMQ 增加了 basicNack 方法以提供批量取消能力。

void basicNack(long deliveryTag,boolean multiple,boolean requeue)throws IOException
Reject one or several received messages. Supply the deliveryTag from the AMQP.Basic.GetOk or AMQP.Basic.GetOk method containing the message to be rejected.
Parameters:
deliveryTag - the tag from the received AMQP.Basic.GetOk or AMQP.Basic.Deliver  相当于这条消息的id
multiple - true to reject all messages up to and including the supplied delivery tag; false to reject just the supplied delivery tag.  true表示不再接受任何消息,false表示不再接收参数deliveryTag表示的这条数据
requeue - true if the rejected message(s) should be requeued rather than discarded/dead-lettered requeue 值为 true 表示该消息重新放回队列头,值为 false 表示放弃这条消息 参考上面方法
Throws:
IOException - if an error is encountered
See Also:
AMQP.Basic.Nack
参考:https://www.cnblogs.com/gordonkong/p/6952957.html?utm_source=itdadao&utm_medium=referral

转载于:https://www.cnblogs.com/longsanshi/p/8143830.html

rabbitMQ 常用api翻译相关推荐

  1. 【小白学PyTorch】7.最新版本torchvision.transforms常用API翻译与讲解

    机器学习炼丹术]的学习笔记分享 <<小白学PyTorch>> 小白学PyTorch | 6 模型的构建访问遍历存储(附代码) 小白学PyTorch | 5 torchvisio ...

  2. 人工智能常用 API

    人工智能常用 API 转载  2016年07月13日 19:17:27 2047 机器学习与预测 1.AlchemyAPI   在把数据由非结构化向结构化的转化中运用得较多.用于社交媒体监控.商业智能 ...

  3. RabbitMQ常用操作命令和入门案例(一对一的简单模式)

    RabbitMQ常用操作命令和入门案例(一对一的简单模式) 一.RabbitMQ常用操作命令 常见命令列表: # 前台启动Erlang VM和RabbitMQrabbitmq-server # 后台启 ...

  4. 【java】简述CGLIB常用API

    1.概述 转载:简述CGLIB常用API 类似:[Spring]CGLIB动态代理 CGLIB,即Code Generation Library,是一个强大的.高性能的代码生成库.其被广泛应用于AOP ...

  5. Delphi 常用API 函数

    Delphi 常用API 函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeIconic ...

  6. [译] 使用谷歌Cloud Translation API翻译文本

    CSDN广告邮件太多了,邮箱已经屏蔽了CSDN,留言请转SegmentFault:https://segmentfault.com/a/1190000014205232 原文:Translation ...

  7. 软件破解中常用API

    原生程序开发者,破解也是会用的,这里转载一些资料: 在软件破解中,常用软件破解方法就是下断点快速找事件,在命令行BP下断点,shift+f9找事件然后再alt+f9返回. 快捷方法,ctrl+A分析代 ...

  8. RabbitMQ的6种工作模式的学习记录,普通MAVEN和springboot项目整合rabbitmq的API详解

    1.RabbitMQ后台管理页面 2.RabbitMQ 核心(自我理解) 3.RabbitMQ6种工作模式介绍 4. RabbitMQ的消息可靠性 5.RabbitMQ普通MAVEN项目使用 6.Sp ...

  9. 【大数据分析必备】超全国内常用API接口汇总

    下面列举了100多个国内常用API接口,并按照 笔记.出行.词典.电商.地图.电影.即时通讯.开发者网站.快递查询.旅游.社交.视频.天气.团队协作.图片与图像处理.外卖.消息推送.音乐.云.语义识别 ...

最新文章

  1. shell /dev/null
  2. python3 判断ip有效性 是否是内网ip
  3. 一篇来自网络的关于“enqueue”events的简短参考(转)
  4. Git之撤销add操作
  5. WordPress函数:get_sidebar(获取侧边栏)
  6. 手把手教你用express搭建个人博客(二)
  7. 张斌教授评《你的知识需要管理》
  8. 大数据知识可以怎么学习
  9. 计算机主机hdmi接口是什么意思,hdmi接口有什么用,教您电脑hdmi接口有什么用
  10. linux服务器22端口不通,怎么解决linux的端口不通问题
  11. 【uniapp】 两种上拉加载方式
  12. LeetCode——6. Z 字形变换
  13. 【Jmeter】分布式测试--单机均衡负载压测
  14. 到底什么是BI?BI能为企业带来什么?
  15. java 与jni转码,Android NDK开发之旅37--FFmpeg转码压缩
  16. 【Python代码基础(符号篇1)】
  17. 用优雅拂去岁月的轻尘
  18. 语句倒装和语音图像压缩的科学原理
  19. 为什么每次登录都有烦人的验证码
  20. 区块链为什么这么火热?

热门文章

  1. 可有可无的Mysql工作技巧 3 -- 工作中用到的理论范式,工具,建模经验
  2. java虚拟机之二虚拟机内存结构
  3. 工程师软技能3:如何学习
  4. Vue mixins学习
  5. laravel 安装后500错误
  6. Java高并发入门-线程初步(二)
  7. 解决方案:超卖(Redis原子队列)
  8. 使用networkx来生成网络科学里面三种常见的网络
  9. skywalking搭建与使用
  10. idea构建spring源码环境