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基础上完整的, ...
随机推荐
- Aborted connection 1055898 to db: 'xxx' user: 'yyy' host: 'xxx.xxx.xxx.xxx' (Got timeout reading communication packets)
mysql错误日志中,发现大量以下类似信息:(mysql 5.7.18) [Note] Aborted connection 1055898 to db: 'xxx' user: 'yyy' host ...
- Andriod下载源码导入后AndroidManifest.xml小红叉的解决办法
问题描述: 下载源码后,用eclipse导入,AndroidManifest.xml小红叉,而且AndroidManifest.xml打不开无法编译,请高人指点! 解决办法: 网上下载的工程文件夹不要 ...
- CSS3选择器之学习笔记
首先说first-child与last-child,这两个选择器很容易明白,就是父元素下的第一个子元素和最后一个子元素.而nth-child和nth-last-child则是父元素下指定序号的子元素, ...
- 第二篇:呈现内容_第二节:WebControl呈现
一.WebControl的呈现过程 WebControl派生自Control类,所以WebControl的呈现功能基于Control的呈现逻辑之上,但有了比较大的扩展. 首先,WebControl重写 ...
- sql server2008系统表详细说明sys.开头的表
一.库里有多少个表,表名等 select * from INFORMATION_SCHEMA.TABLES 二.查询表.字段.类型.是否主键.长度.小数位.是否允许空.默认值等详细查询 效果: SEL ...
- jQueryUI modal dialog does not show close button (x) JQueryUI和BootStrap混用时候,右上角关闭按钮显示不出图标的解决办法
I had this problem and was able to resolve it with the declaration below. $.fn.bootstrapBtn = $.fn.b ...
- WPF 4 Ribbon 开发 之 快捷工具栏(Quick Access Toolbar)
转自 http://www.cnblogs.com/gnielee/archive/2010/05/10/wpf4-ribbon-quick-access-toolbar.html 在Office 2 ...
- 使用vs的查找功能,简单大概的统计vs中的代码行数
VS强大的查找功能,可以使用正则表达式来进行查找,这里统计代码行数的原理就是: 在所有指定文件中进行搜索,统计匹配的文本行数. 但是匹配的行需要满足:非注释.非空等特殊非代码行. 使用Ctrl+Shi ...
- [LintCode] Find the Connected Component in the Undirected Graph
Find the Connected Component in the Undirected Graph Find the number connected component in the undi ...
- jmeter经验----java 读取文件中文乱码问题
很少写java io的代码,今天整了一个发现 本地调试好好的,放到jmeter里就打印乱码.一番折腾,终于搞定~直接上代码: List<Order> orderList = new Arr ...