RabbitMQ文档翻译——Hello World!(下)
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中运行:

源码:
package Consuming; import com.rabbitmq.client.*; import java.io.IOException; /**
* Created by zhengbin06 on 16/9/11.
*/
public class Recv {
private final static String QUEUE_NAME = "hello"; public static void main(String[] argv)
throws java.io.IOException,
java.lang.InterruptedException { ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
// factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
RabbitMQ文档翻译——Hello World!(下)的更多相关文章
- RabbitMQ,Windows环境下安装搭建
切入正题:RabbitMQ的Windows环境下安装搭建 一.首先安装otp_win64_20.1.exe,,, 二.然后安装,rabbitmq-server-3.6.12.exe, 安装完成后,在服 ...
- (三)RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1
原文:(三)RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1 如果你看过前两章对RabbitMQ已经有了一定了解,现在已经摩拳擦掌,来吧动手吧! 用什么系统 本文使用的是Cen ...
- RabbitMQ 在 Win10 环境下的安装与配置
1 RabbitMQ 环境配置 1.1 ErLang 下载安装 RabbitMQ 需要 ErLang 环境支持:首先下载 ErLang 并安装. 建议使用新版本,版本过低存在与 Rab ...
- RabbitMQ在Windows环境下的安装与使用
Windows下安装RabbitMQ 环境配置 部署环境 部署环境:windows server 2008 r2 enterprise 官方安装部署文档:http://www.rabbitmq.com ...
- RabbitMQ学习在windows下安装配置
RabbitMQ学习一. 在windows下安装配置 1.下载并安装erlang,http://www.erlang.org/download.html,最新版是R15B01(5.9.1).由于我机器 ...
- 解决spring boot在RabbitMQ堆积消息情况下无法启动问题
最近遇到一个问题,服务站点上线之前,先去新建需要的rabbitmq并绑定关系,此时 如果发送消息方运行, 那边会造成新建的q消息部分堆积得不到及时消费 那么问题来了? 在消息堆积情况下,服务站点无法启 ...
- RabbitMQ介绍及windows下安装使用
RebbitMQ介绍 RabbitMQ是一个由 Erlang (一种通用的面向并发的编程语言)开发的AMQP(Advanced Message Queue )的开源实现,Rabbit MQ 是建立在E ...
- RabbitMQ(一):Windows下RabbitMQ安装
1.Windows下安装RabbitMQ需要以下几个步骤 (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang. ...
- RabbitMQ在windows环境下的安装
最近一直想入手一台电脑,作为linux服务器,由于经济状况也没有入手,现在就先介绍windows环境下安装rabbitMQ. RabbitMQ是什么 ? RabbitMQ是一个在AMQP基础上完整的, ...
随机推荐
- SharePoint 2013 Step by Step—— 为终端用户提供故障恢复的解决方案 Part I
Disaster Recovery,我把他直译"故障恢复",或者也可以翻译成 "灾难复原 ".光字面意思就可以领会到,当SharePoint Server发生了 ...
- Exception的妙用
实际工作中遇到的一个例子: 一.看这样一个方法: /** 传入以微秒(us)为单位的时间字符串,转换成可读的(年-月-日 时:分:秒)日期格式*/ public String getDateStrin ...
- [转]MVC 框架教程
Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活.松散耦合的 web 应用程序的组件.MVC 模式导致了应用程序的不同方面(输入逻辑.业务逻辑和 UI 逻辑)的分离 ...
- 比较@Resource、@Autowired
@Resource @Resource默认按byName自动注入.既不指定name属性,也不指定type属性,则自动按byName方式进行查找.如果没有找到符合的bean,则回退为一个原始类型进行进行 ...
- python标准库介绍——33 thread 模块详解
?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...
- IDEA快捷键收集
生成set 和get方法 .生产重写方法Alt+Insert 查看类的所有方法alt + 7 去掉多余的引用包alt + ctrl + O ctrl + alt + t 生成try 语句 自动导入包 ...
- 大家来找茬:富连网今天中午抢购二手iPhone时网站无法访问的问题
前几天在新闻区看到富士康卖二手iPhone的新闻,今天又看到说今天中午12点开抢.一大早就发现富连网无法访问了.前几天刚看到新闻的时候注册了个账号进去看了看,发现页面加载速度非常慢,今天中午基本无法打 ...
- AR模型与数据平稳性之间的关系
作者:桂. 时间:2017-12-19 21:39:08 链接:http://www.cnblogs.com/xingshansi/p/8068021.html 前言 前几天碰到一个序列分析的问题, ...
- kail-linux下安装pycharm
1.下载pycharm pycharm官网下载链接:https://www.jetbrains.com/pycharm/download/#section=linux 请注意下载对应系统的profes ...
- 怎么运行 ASP.NET Core控制台程序
aspnet test.dll