RabbitMQ学习之:(九)Headers Exchange (转贴+我的评论)
From: http://lostechies.com/derekgreer/2012/05/29/rabbitmq-for-windows-headers-exchanges/
RabbitMQ for Windows: Headers Exchanges
This is the eighth and final installment to the series: RabbitMQ for Windows. In thelast installment, we walked through creating a topic exchange example. As the last installment, we’ll walk through a headers exchange example.
Headers exchanges examine the message headers to determine which queues a message should be routed to. As discussed earlier in this series, headers exchanges are similar to topic exchanges in that they allow you to specify multiple criteria, but offer a bit more flexibility in that the headers can be constructed using a wider range of data types (1).
To subscribe to receive messages from a headers exchange, a dictionary of headers is specified as part of the binding arguments. In addition to the headers, a key of “x-match” is also included in the dictionary with a value of “all”, specifying that messages must be published with all the specified headers in order to match, or “any”, specifying that the message needs to only have one of the specified headers specified.
As our final example, we’ll create a Producer application which publishes the message “Hello, World!” using a headers exchange. Here’s our Producer code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using RabbitMQ.Client;
using RabbitMQ.Client.Framing.v0_9_1; namespace Producer
{
class Program
{
const string ExchangeName = "header-exchange-example"; static void Main(string[] args)
{
var connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "localhost"; IConnection connection = connectionFactory.CreateConnection();
IModel channel = connection.CreateModel();
channel.ExchangeDeclare(ExchangeName, ExchangeType.Headers, false, true, null);
byte[] message = Encoding.UTF8.GetBytes("Hello, World!"); var properties = new BasicProperties();
properties.Headers = new Dictionary<string, object>();
properties.Headers.Add("key1", "12345");
TimeSpan time = TimeSpan.FromSeconds(10);
var stopwatch = new Stopwatch();
Console.WriteLine("Running for {0} seconds", time.ToString("ss"));
stopwatch.Start();
var messageCount = 0; while (stopwatch.Elapsed < time)
{
channel.BasicPublish(ExchangeName, "", properties, message);
messageCount++;
Console.Write("Time to complete: {0} seconds - Messages published: {1}\r", (time - stopwatch.Elapsed).ToString("ss"), messageCount);
Thread.Sleep(1000);
} Console.Write(new string(' ', 70) + "\r");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
message = Encoding.UTF8.GetBytes("quit");
channel.BasicPublish(ExchangeName, "", properties, message);
connection.Close();
}
}
}
In the Producer, we’ve used a generic dictionary of type Dictionary<string, object> and added a single key “key1” with a value of “12345”. As with our previous example, we’re using a stopwatch as a way to publish messages continually for 10 seconds.
For our Consumer application, we can use an “x-match” argument of “all” with the single key/value pair specified by the Producer, or we can use an “x-match” argument of “any” which includes the key/value pair specified by the Producer along with other potential matches. We’ll use the latter for our example. Here’s our Consumer code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events; namespace Consumer
{
class Program
{
const string QueueName = "header-exchange-example";
const string ExchangeName = "header-exchange-example"; static void Main(string[] args)
{
var connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "localhost"; IConnection connection = connectionFactory.CreateConnection();
IModel channel = connection.CreateModel();
channel.ExchangeDeclare(ExchangeName, ExchangeType.Headers, false, true, null);
channel.QueueDeclare(QueueName, false, false, true, null); IDictionary specs = new Dictionary();
specs.Add("x-match", "any");
specs.Add("key1", "12345");
specs.Add("key2", "123455");
channel.QueueBind(QueueName, ExchangeName, string.Empty, specs);
// 注意,这个StartConsume是我们写的函数,其实他是在一个循环内反复调用CallBack函数。
channel.StartConsume(QueueName, MessageHandler);
connection.Close();
} public static void MessageHandler(IModel channel, DefaultBasicConsumer consumer, BasicDeliverEventArgs eventArgs)
{
string message = Encoding.UTF8.GetString(eventArgs.Body);
Console.WriteLine("Message received: " + message);
foreach (object headerKey in eventArgs.BasicProperties.Headers.Keys)
{
Console.WriteLine(headerKey + ": " + eventArgs.BasicProperties.Headers[headerKey]);
} if (message == "quit")
channel.BasicCancel(consumer.ConsumerTag);
}
}
}
Rather than handling our messages inline as we’ve done in previous examples, this example uses an extension method named StartConsume() which accepts a callback to be invoked each time a message is received. Here’s the extension method used by our example:
using System;
using System.IO;
using System.Threading;
using RabbitMQ.Client;
using RabbitMQ.Client.Events; namespace Consumer
{
public static class ChannelExtensions
{
public static void StartConsume(this IModel channel, string queueName, Action<IModel, DefaultBasicConsumer, BasicDeliverEventArgs> callback)
{
var consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(queueName, true, consumer); while (true)
{
try
{
var eventArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
new Thread(() => callback(channel, consumer, eventArgs)).Start();
}
catch (EndOfStreamException)
{
// The consumer was cancelled, the model closed, or the connection went away.
break;
}
}
}
}
}
Setting our solution to run both the Producer and Consumer applications upon startup, running our example produces output similar to the following:
Producer
Running for 10 seconds
Time to complete: 08 seconds - Messages published: 2
Consumer
Message received: Hello, World!
key1: 12345
Message received: Hello, World!
key1: 12345
That concludes our headers exchange example as well as the RabbitMQ for Windows series. For more information on working with RabbitMQ, see the documentation athttp://www.rabbitmq.com or the purchase the book RabbitMQ in Action by Alvaro Videla and Jason Williams. I hope you enjoyed the series.
Footnotes:
1 – See http://www.rabbitmq.com/amqp-0-9-1-errata.html#section_3 andhttp://hg.rabbitmq.com/rabbitmq-dotnet-client/diff/4def852523e2/projects/client/RabbitMQ.Client/src/client/impl/WireFormatting.csfor supported field types.
RabbitMQ学习之:(九)Headers Exchange (转贴+我的评论)的更多相关文章
- rabbitmq学习(九) —— 关于消息队列的选型
转自http://cmsblogs.com/?p=3846 在IM这种讲究高并发.高消息吞吐的互联网场景下,MQ消息中间件是个很重要的基础设施,它在IM系统的服务端架构中担当消息中转.消息削峰.消息交 ...
- (九)RabbitMQ消息队列-通过Headers模式分发消息
原文:(九)RabbitMQ消息队列-通过Headers模式分发消息 Headers类型的exchange使用的比较少,以至于官方文档貌似都没提到,它是忽略routingKey的一种路由方式.是使用H ...
- RabbitMQ学习之:(六)Direct Exchange (转贴+我的评论)
From: http://lostechies.com/derekgreer/2012/04/02/rabbitmq-for-windows-direct-exchanges/ RabbitMQ fo ...
- RabbitMQ入门教程(九):首部交换机Headers
原文:RabbitMQ入门教程(九):首部交换机Headers 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...
- RabbitMQ学习系列(四): 几种Exchange 模式
上一篇,讲了RabbitMQ的具体用法,可以看看这篇文章:RabbitMQ学习系列(三): C# 如何使用 RabbitMQ.今天说些理论的东西,Exchange 的几种模式. AMQP协议中的核心思 ...
- RabbitMQ headers Exchange
Headers Exchange headers也是一种交换机类型,但是在rabbitmq官网中的教程中并没有说到.资料也很少,但是找一找总会有的. headers与direct的模式不同,不是使用r ...
- RabbitMQ学习总结 第六篇:Topic类型的exchange
目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...
- (转)RabbitMQ学习之Headers交换类型(java)
http://blog.csdn.net/zhu_tianwei/article/details/40923131 Headers类型的exchange使用的比较少,它也是忽略routingKey的一 ...
- RabbitMQ学习之:(七)Fanout Exchange (转贴+我的评论)
From:http://lostechies.com/derekgreer/2012/05/16/rabbitmq-for-windows-fanout-exchanges/ PunCha: Ther ...
随机推荐
- CAFFE(二):Ubuntu 下安装OpenCv 3.4.1
一步:进入OpenCv官网 选择 3.4.1 版本的 source , 下载 opencv-3.4.1.zip ,如下图选择Sources下载 解压缩到home目录.并执行如下代码: { cd ~/o ...
- 2.01_Python网络爬虫概述
一:什么是网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取网络信息的程序或者脚本: 二:为什么要做网络爬虫? 大数据时代 ...
- LoadRunner(4)
一.LoadRunner工具的组成 1.VuGen 虚拟用户脚本生成器 脚本好比:武器 VuGen好比:兵工厂 VU好比:士兵 2.Controller 压力调度控制台 好比:总指挥部 3.Analy ...
- windows 10安装mongodb数据库
简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当 ...
- summernote 富文本编辑器限制输入字符长度
项目中需要一个比较简单的富文本编辑器,于是选中了summernote .虽然比较轻量,但是在开发中也遇到了几个问题,在此记录一下. 1:样式和bootstrap冲突,初始化之后显示为: .note-e ...
- (四)关于java.lang.IllegalMonitorStateException异常说明
1.异常原因及解释 首先你要了解这个异常为什么会抛出,这个异常会在三种情况下抛出:1>当前线程不含有当前对象的锁资源的时候,调用obj.wait()方法;2>当前线程不含有当前对象的锁资源 ...
- Mysql中用exists代替in
exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的 ...
- P2709 小B的询问——普通莫队&&模板
普通莫队概念 莫队:莫涛队长发明的算法,尊称莫队.其实就是优化的暴力. 普通莫队只兹磁询问不支持修改,是离线的. 莫队的基本思想:就是假定我得到了一个询问区间[l,r]的答案,那么我可以在极短(通常是 ...
- WHU 583 Palindrome ( 回文自动机 && 本质不同的回文串的个数 )
题目链接 题意 : 给你一个串.要你将其划分成两个串.使得左边的串的本质不同回文子串的个数是右边串的两倍.对于每一个这样子的划分.其对答案的贡献就是左边串的长度.现在要你找出所有这样子的划分.并将贡献 ...
- 百度AI接口调用
创建应用 登录网站 登录www.ai.baidu.com 进入控制台 进入语音技术 创建应用 管理应用 技术文档 SDK开发文档 接口能力 版本更新记录 注意事项 目前本SDK的功能同REST API ...