Receiving

That's it for our sender. Our receiver is pushed messages from RabbitMQ, so unlike the sender which pushlishes a single message, we'll keep it running to listen for message and print them out.

译:这就是我们的消息发送者。我们的消息接收者是通过RabbitMQ的消息推送接收到消息,所以不同于消息发送者那样只是简单的发送一条消息,我们需要保持它一直运行,就像一个监听器一样,用来不停的接收消息,并把消息输出出来。


The extra DefaultConsumer is a class implementing the Consumer interface we'll use to buffer the messages pushed to us by the server.

译:特别注意这个DefaultConsume,这个类是Consumer接口的实现,我们用它来缓冲服务器推送给我们的消息。


Setting up is the same as the sender; we open a connection and a channel, and declare the queue from which we're going to consume. Note this matches up with the queue that send publishes to.

译:下面的设置与sender差不多;我们首先建立一个连接connection和一个通道channel,再声明一个我们要进行消费的消息队列queue。注意这与发送到消息队列相匹配。


Note that we declare the queue here, as well. Because we might start the receiver before the sender, we want to make sure the queue exists before we try to consume messages from it.

译:注意,这里我们同样的声明一个消息队列。因为我们应该在开启发送者之前先开启接受者,我们需要确定在我们试图从消息队列中获取消息时,消息队列已经存在。


We're about to tell the server to deliver us the messages from the queue.Since it will push us messages asynchronously, we provide a callback in the form of an object that will buffer the messages until we're ready to use them. That is what a DefaultConsumer subclass does.

译:我们将要告诉服务器可以从消息队列释放消息释放给我们了。之后它就会异步的将消息发送给我们,我们提供回调函数callback的形式缓冲消息,直到我们准备使用这些消息的时候。这就是DefaultConsumer做的事情。

在IDE中运行:

源码:

 1 package Consuming;
 2
 3 import com.rabbitmq.client.*;
 4
 5 import java.io.IOException;
 6
 7 /**
 8  * Created by zhengbin06 on 16/9/11.
 9  */
10 public class Recv {
11     private final static String QUEUE_NAME = "hello";
12
13     public static void main(String[] argv)
14             throws java.io.IOException,
15             java.lang.InterruptedException {
16
17         ConnectionFactory factory = new ConnectionFactory();
18         factory.setHost("localhost");
19 //        factory.setPort(5672);
20         Connection connection = factory.newConnection();
21         Channel channel = connection.createChannel();
22         channel.queueDeclare(QUEUE_NAME, false, false, false, null);
23         System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
24         Consumer consumer = new DefaultConsumer(channel) {
25             @Override
26             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
27                     throws IOException {
28                 String message = new String(body, "UTF-8");
29                 System.out.println(" [x] Received '" + message + "'");
30             }
31         };
32         channel.basicConsume(QUEUE_NAME, true, consumer);
33     }
34 }

View Code

转载于:https://www.cnblogs.com/zhengbin/p/5861989.html

RabbitMQ文档翻译——Hello World!(下)相关推荐

  1. RabbitMQ在Windows环境下的服务启动问题

    RabbitMQ在Windows环境下的服务启动问题 用rabbitmq_server-3.8.4.exe安装完成后(选择安装服务项),这个时候在浏览器输入http://localhost:15672 ...

  2. RabbitMQ(四) Work模式下的消息产生以及消费代码实现示例

    在工作队列中,我们有多个消息的消费者,每个消费者都会进行消息消费,在默认情况下,RabbitMQ会进行消息轮询发送给每一个消费者,因此每个消费者处理的消息数量是一致的.下面直接看我们的主要文件代码 一 ...

  3. RabbitMQ在Windows环境下部署(简单有效)

    环境 win10_x64 安装步骤 由于RabbitMQ服务器是用Erlang语言编写,所以我们需要先安装Erlang环境 OTP_win64_v22.2 安装步骤默认就行 在官方的git中下载指定的 ...

  4. Rabbitmq学习笔记007---Centos7下安装rabbitmq_测试通过

    JAVA技术交流QQ群:170933152 1.在Centos下安装rabbitmq,之前都是在windows中安装的,centos下安装还挺麻烦 这里介绍两种安装方法: 第一种,我测试了,太慢了,不 ...

  5. rabbitMQ概述/在springboot下测试五种模式

    一.应用场景: (1) 异步操作: 任务异步处理将不需要同步处理的并且耗时长的操作由消息队列通知消息接收方进行异步处理.提高了应用程序的响应时间. (2) 解耦: 应用程序解耦合MQ相当于一个中介,生 ...

  6. RabbitMQ在windows10环境下安装步骤

    下载并安装Erlang RabbitMQ服务端代码是使用并发式语言Erlang编写的,安装Rabbit MQ的前提是安装Erlang. Erlang官网下载地址:http://www.erlang.o ...

  7. RabbitMQ 在 Win10 环境下的安装与配置

    文章目录 1. RabbitMQ 环境配置(ErLang 10.4 + MQ 3.7.17) 1.1 ErLang 下载安装 1.2 RabbitMQ 下载安装 1.3 环境变量配置 1.3.1 新建 ...

  8. RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1

    如果你看过前两章对RabbitMQ已经有了一定了解,现在已经摩拳擦掌,来吧动手吧! 用什么系统 本文使用的是Centos7,为了保证对linux不太熟悉的伙伴也能轻松上手(避免折在安装的路上),下面是 ...

  9. RabbitMQ基本管理(下)

    为了可以登陆RabbitMQ,必须创建RabbitMQ用户账号. # rabbitmqctl add_user elite elite123 Creating user "elite&quo ...

  10. RabbitMQ实例教程:Windows下安装RabbitMQ

    (1)下载RabbitMQ服务器 从RabbitMQ官网下载最新的稳定版.目前最新版本为3.5.1. (2)移除RabbitMQ老版本. 如果之前安装了老版本的话,或者想要将Erlang VM从32位 ...

最新文章

  1. 李飞飞团队加入AI抗疫:家用监控系统,可以远程反馈新冠症状
  2. Python基础教程:为元组中的每一个元素命名
  3. golang mutex互斥锁分析
  4. 关于可迭代对象、迭代器和生成器
  5. 从WebRtc学习RTP协议
  6. 寒冬袭来,带你使用Flask开发一款天气查询软件吧
  7. centos6.9安装虚拟机kvm
  8. Aspen ONE Suite 11.0 软件下载及其安装教程
  9. win10很多软件显示模糊_win10系统字体模糊的解决方法
  10. bili弹幕姬_bilibili弹幕姬怎么用 - 卡饭网
  11. win7禁止应用程序联网
  12. Java自学教程百度云盘,疯狂加持!腾讯大佬的
  13. SRP Batcher:提升您的渲染性能
  14. vuepress-theme-reco自动生成侧边栏
  15. 对圆柱面的曲面积分_积分曲面为圆柱面的曲面积分的计算
  16. 埋点 神策小程序_神策埋点思路
  17. 这是新技术时代来临,却始终都无法打开局面的“紧箍咒”
  18. Remoting简单实例[]
  19. 【Linux】GCC程序开发工具(下)
  20. java中获取输入的几种方式

热门文章

  1. 全球IPv4地址正式耗尽!
  2. 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
  3. 年薪 40w 的应届生
  4. python基础之psutil模块和发邮件(smtplib和yagmail)
  5. NOIP2016提高A组 B题 【HDU3072】【JZOJ4686】通讯
  6. 图解C#的值类型,引用类型,栈,堆,ref,out
  7. Delphi / Pascal 语法知识干货
  8. rabbitmq 笔记
  9. CodeForces 489B (贪心 或 最大匹配) BerSU Ball
  10. Asp.Net Repeater控件绑定泛型ListT的用法