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!(下)的更多相关文章

  1. RabbitMQ,Windows环境下安装搭建

    切入正题:RabbitMQ的Windows环境下安装搭建 一.首先安装otp_win64_20.1.exe,,, 二.然后安装,rabbitmq-server-3.6.12.exe, 安装完成后,在服 ...

  2. (三)RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1

    原文:(三)RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1 如果你看过前两章对RabbitMQ已经有了一定了解,现在已经摩拳擦掌,来吧动手吧! 用什么系统 本文使用的是Cen ...

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

    1 RabbitMQ 环境配置 1.1 ErLang 下载安装     RabbitMQ 需要 ErLang 环境支持:首先下载 ErLang 并安装.     建议使用新版本,版本过低存在与 Rab ...

  4. RabbitMQ在Windows环境下的安装与使用

    Windows下安装RabbitMQ 环境配置 部署环境 部署环境:windows server 2008 r2 enterprise 官方安装部署文档:http://www.rabbitmq.com ...

  5. RabbitMQ学习在windows下安装配置

    RabbitMQ学习一. 在windows下安装配置 1.下载并安装erlang,http://www.erlang.org/download.html,最新版是R15B01(5.9.1).由于我机器 ...

  6. 解决spring boot在RabbitMQ堆积消息情况下无法启动问题

    最近遇到一个问题,服务站点上线之前,先去新建需要的rabbitmq并绑定关系,此时 如果发送消息方运行, 那边会造成新建的q消息部分堆积得不到及时消费 那么问题来了? 在消息堆积情况下,服务站点无法启 ...

  7. RabbitMQ介绍及windows下安装使用

    RebbitMQ介绍 RabbitMQ是一个由 Erlang (一种通用的面向并发的编程语言)开发的AMQP(Advanced Message Queue )的开源实现,Rabbit MQ 是建立在E ...

  8. RabbitMQ(一):Windows下RabbitMQ安装

    1.Windows下安装RabbitMQ需要以下几个步骤 (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang. ...

  9. RabbitMQ在windows环境下的安装

    最近一直想入手一台电脑,作为linux服务器,由于经济状况也没有入手,现在就先介绍windows环境下安装rabbitMQ. RabbitMQ是什么 ? RabbitMQ是一个在AMQP基础上完整的, ...

随机推荐

  1. SQL Tuning Advisor

    SQL Tuning Advisorsql调优顾问可提供的建议有:-收集对象的统计信息-创建索引-重写sql语句-创建sql profile-创建sql plan baseline SQL Tunin ...

  2. 机器学习---支持向量机(SVM)

    非常久之前就学了SVM,总认为不就是找到中间那条线嘛,但有些地方模棱两可,真正编程的时候又是一团浆糊.參数任意试验,毫无章法.既然又又一次学到了这一章节,那就要把之前没有搞懂的地方都整明确,嗯~ 下面 ...

  3. 关于thymeleaf+layout布局的使用方式,spring boot 访问页面(静态页面及jsp页面)

    首先建立相同部分的html,这里我命名为layout.html,放在了`templates/layout'文件夹下,这个路径以后是会用到的,以下是我的layout的代码,比较粗糙. 但是应该会更好的帮 ...

  4. js评价五星

    js评价五星 1.图片(star.png): 2.图片和html文件在同级目录 <html> <head> <script src="http://libs.b ...

  5. 【Unity】第12章 导航网格和寻路

    开发环境:Win10.Unity5.3.4.C#.VS2015 创建日期:2016-05-09 一.简介 NavMesh(导航网格)是3D游戏世界中用于实现"动态"物体自动寻路的一 ...

  6. 【Unity】6.1 Unity中的C#脚本基础知识

    分类:Unity.C#.VS2015 创建日期:2016-04-16 一.简介 1.常用的C#数据类型 这里简单介绍用Unity开发游戏时,最常用的一些数据类型. (1)基本类型 int.float. ...

  7. openssl之EVP系列之13---EVP_Open系列函数介绍

    openssl之EVP系列之13---EVP_Open系列函数介绍     ---依据openssl doc/crypto/EVP_OpenInit.pod翻译和自己的理解写成     (作者:Dra ...

  8. android - 调用系统分享功能分享图片

    step1: 编写分享代码, 将Uri的生成方式改为由FileProvider提供的临时授权路径,并且在intent中添加flag 注意:在Android7.0之后,调用系统分享,传入URI的时候可能 ...

  9. Android-一只手指滑动View,另一只手指按Home键,重新进入后View状态无法更新的问题

    上午测试报了一个bug:说是一只手指拖动虚拟摇杆上的View滑块不松,另一只手指点击Home键将App压后台,重新进入的时候,View滑块卡死了. 刚开始看到这个问题感觉很奇怪,因为正常情况下,手指离 ...

  10. Fetch API 接口参考

    前言 Fetch API是新的ajax解决方案,用于解决古老的XHR对象不能实现的问题,Fetch API 提供了一个获取资源的接口(包括跨域请求),任何使用过 XMLHttpRequest 的人都能 ...