rabbitmq AmqpClient 使用Topic 交换机同一个channel 同时多个队列 ,多个交换机,C++代码示例
// 消息发送
bool PublishExchangeTopicMulti(const std::string &strUri)
{
AmqpClient::Channel::ptr_t channel =
AmqpClient::Channel::CreateFromUri(strUri); if (channel == nullptr)
{
return false;
} // 声明交换机,若不存在则创建
std::string strTopicExchange1 = "topic_exchange_1";
std::string strTopicExchange2 = "topic_exchange_2";
channel->DeclareExchange(strTopicExchange1, AmqpClient::Channel::EXCHANGE_TYPE_TOPIC);
channel->DeclareExchange(strTopicExchange2, AmqpClient::Channel::EXCHANGE_TYPE_TOPIC); while (true)
{
// 可输入例如 "topic_exchange_1 disk.info 666"
// "topic_exchange_2 any.warning 123"
std::cout << "请输入[exchange] [routing_key1.routing_key2] [message]: " << std::endl; std::string strExchange;
std::string severity;
std::string message;
std::cin >> strExchange;
std::cin >> severity;
std::cin >> message; channel->BasicPublish(strExchange, severity,
AmqpClient::BasicMessage::Create(message)); std::cout << "[X] to " << strExchange << ", send "
<< severity << ": " << message << std::endl;
} } void ReceiveTopicExchangeMulti(const std::string &strUri)
{
AmqpClient::Channel::ptr_t channel =
AmqpClient::Channel::CreateFromUri(strUri); if (channel == nullptr)
{
return ;
} // 这里我们声明两个交换机,类型均为topic , 我们将通过不同的队列从两个交换机中取消息。
// 这里交换机的名称需要与发送端的保持一致。
std::string strTopicExchange1 = "topic_exchange_1";
std::string strTopicExchange2 = "topic_exchange_2";
channel->DeclareExchange(strTopicExchange1, AmqpClient::Channel::EXCHANGE_TYPE_TOPIC);
channel->DeclareExchange(strTopicExchange2, AmqpClient::Channel::EXCHANGE_TYPE_TOPIC); // 这里我们声明了三个队列,第一个队列从交换机1 取数据第二三个队列从交换机2 取数据;
// 但是第二三个队列所绑定的routing_key 有所不同。
// 当然了,routing_key 也可以相同,这样的话相同routing_key 的消息就会在两个队列中都出现。
std::string strTopicQueue_1 = "topic_queue_1";
std::string strTopicQueue_2 = "topic_queue_2";
std::string strTopicQueue_3 = "topic_queue_3";
// 第一个参数若为空,则系统默认生成随机队列名称
// 第三个参数 表明队列是否持久的。true: 服务器重启将会保留该Exchange,
// 警告,若只设置此项,不代表消息的持久化。即不保证重启后消息还在。
channel->DeclareQueue(strTopicQueue_1, false, true, false, false);
channel->DeclareQueue(strTopicQueue_2, false, true, false, false);
channel->DeclareQueue(strTopicQueue_3, false, true, false, false); // 队列绑定我们感兴趣的routing_key, 表示我们只接收这些routing_key 相关的消息。
channel->BindQueue(strTopicQueue_1, strTopicExchange1, "*.info");
channel->BindQueue(strTopicQueue_1, strTopicExchange1, "disk.*");
channel->BindQueue(strTopicQueue_1, strTopicExchange1, "info.error"); // 在交换机2 上面我们绑定了队列2 和队列3 。但是它们所关心的routing_key 不同。
channel->BindQueue(strTopicQueue_3, strTopicExchange2, "*.info");
channel->BindQueue(strTopicQueue_3, strTopicExchange2, "disk.*");
channel->BindQueue(strTopicQueue_2, strTopicExchange2, "info.error"); // 创建消费者标志,这个在后面会告诉 channel 我们需要哪些队列中的相关routing_key 的消息。
// BasicConsume() 第五个参数是指该消息是否以独占的方式处理,若是则不允许第二个消费者绑定到该队列 上,
// 若否,则多个消费者同时绑定到该队列,那么 在该队列上的消息将随机分配到某一个消费者。
// 即,同一个消息将不会同时出现 在两个消费者身上。
std::string strFlagConsume_1 = "tab_consume_1";
std::string strFlagConsume_2 = "tab_consume_2";
std::string strFlagConsume_3 = "tab_consume_3";
channel->BasicConsume(strTopicQueue_1, strFlagConsume_1, true, false, true);
channel->BasicConsume(strTopicQueue_2, strFlagConsume_2, true, false, true);
channel->BasicConsume(strTopicQueue_3, strFlagConsume_3, true, false, true);
// BasicConsume() 的第4 个参数为false 表示,我们需要主动ack 服务器才将该消息清除。 std::vector<std::string> vecFlagConsume;
vecFlagConsume.push_back(strFlagConsume_1);
vecFlagConsume.push_back(strFlagConsume_2);
vecFlagConsume.push_back(strFlagConsume_3); while (true)
{
AmqpClient::Envelope::ptr_t envelope = channel->BasicConsumeMessage(vecFlagConsume); std::string strExchange = envelope->Exchange();
std::string strFlagConsume = envelope->ConsumerTag();
std::string severity = envelope->RoutingKey();
std::string buffer = envelope->Message()->body(); std::cout << "[Y] exchange: " << strExchange << ", flagconsume: " << strflagConsume
<< ", receive " << severity << ": " << buffer << std::endl; channel->BasicAck(envelope);
} for (size_t i = ; i < vecFlagConsume.size(); ++i)
channel->BasicCancel(vecFlagConsume[i]);
}
rabbitmq AmqpClient 使用Topic 交换机同一个channel 同时多个队列 ,多个交换机,C++代码示例的更多相关文章
- rabbitmq AmqpClient 使用Topic 交换机投递与接收消息,C++代码示例
// strUri = "amqp://guest:guest@192.168.30.11:8820/test" // strUri = "amqp://[帐户名]:[密 ...
- 7.RabbitMQ系列之topic主题交换器
topic主题交换器它根据在队列绑定的路由键和路由模式通配符匹配将消息路由到队列. 生产者在消息头中添加路由键并将其发送到主题交换器. 收到消息后,exchange尝试将路由键与绑定到它的所有队列的绑 ...
- RabbitMQ基础学习笔记(C#代码示例)
一.定义: MQ是MessageQueue,消息队列的简称(是流行的开源消息队列系统,利用erlang语言开发).MQ是一种应用程序对应用程序的通信方法.应用程序通过读写入队和出队的消息来通信,无需专 ...
- RabbitMQ的使用(五)RabbitMQ Java Client简单生产者、消费者代码示例
pom文件: <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artif ...
- RabbitMQ 入门系列:9、扩展内容:死信队列:真不适合当延时队列。
系列目录 RabbitMQ 入门系列:1.MQ的应用场景的选择与RabbitMQ安装. RabbitMQ 入门系列:2.基础含义:链接.通道.队列.交换机. RabbitMQ 入门系列:3.基础含义: ...
- RabbitMQ 入门系列:10、扩展内容:延时队列:延时队列插件及其有限的适用场景(系列大结局)。
系列目录 RabbitMQ 入门系列:1.MQ的应用场景的选择与RabbitMQ安装. RabbitMQ 入门系列:2.基础含义:链接.通道.队列.交换机. RabbitMQ 入门系列:3.基础含义: ...
- rabbitmq AmqpClient 使用Direct 交换机投递与接收消息,C++代码示例
// 以DIRECT 交换机和ROUTING_KEY的方式进行消息的发布与订阅 // send // strUri = "amqp://guest:guest@192.168.30.11:8 ...
- rabbitmq AmqpClient 使用Fanout 交换机投递与接收消息,C++代码示例
fanout交换器重点内容非常简单.它只会将接收到的所有消息广播发送到它所知道的所有队列. 投递消息到交换机: #include "SimpleAmqpClient/SimpleAmqpCl ...
- RabbitMQ通过Exchange.topic 对routingkey 进行正则表达式匹配
消费者: static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory() { HostNam ...
随机推荐
- 2019-8-31-C#-遍历枚举
title author date CreateTime categories C# 遍历枚举 lindexi 2019-08-31 16:55:58 +0800 2018-03-13 20:42:2 ...
- 第6篇如何访问pod
一.通过 Service 访问 Pod: 我们不应该期望 Kubernetes Pod 是健壮的,而是要假设 Pod 中的容器很可能因为各种原因发生故障而死掉.Deployment 等 con ...
- 转载 Struts2之------Action类中的get,set方法和execute方法的使用规范和使用流程(规范是没有理由的,必须遵守!!!)
1,Action中get,set方法的使用流程? 前台form中有一个<input type="text" name="username"/> 如果 ...
- webpack 模块热替换的理解和使用
模块热替换(webpack文档上也叫 Hot Module Replacement 或 HMR)是 webpack 提供的最有用的功能之一.它允许在运行时更新各种模块,而无需进行完全刷新. 这句话其实 ...
- 微信支持的Authorization code授权模式(公众号开发)(开放平台资料中心中的代公众号发起网页授权)
链接:https://blog.csdn.net/ASZJBGD/article/details/82838356 主要流程分为两步: 1.获取code 2.通过code换取accesstoken 流 ...
- linux-usb软件系统架构
1.软件系统架构 USB主控制器,芯片里面自带了得.为了让USB主控制器运行,所有有USB主控制器驱动. USB核心,内核提供好的USB协议之类的.USB设备驱动是针对插到接口的设备去工作的软件. 主 ...
- Maven进行自动构建
一个很常见的错误就是路径问题,要把jdk放到java工程的路径里,之前一直默认是jre https://blog.csdn.net/lslk9898/article/details/73836745 ...
- AcWing 226. 233矩阵 (矩阵快速幂+线性递推)打卡
题目:https://www.acwing.com/problem/content/228/ 题意:有一个二维矩阵,这里只给你第一行和第一列,要你求出f[n][m],关系式有 1, f[0][ ...
- (转)使用OpenGL显示图像(二)定义Shapes
定义形状 编写:jdneo - 原文:http://developer.android.com/training/graphics/opengl/shapes.html 在一个OpenGL ES Vi ...
- UE格式化XML文件
在UE中如何格式化xml:如果xml文件不是格式化的,应该“试图”-->“查看方式”-->“xml”:然后再“格式”-->“xml转换到回车符”.具体再要的属性,自己去摸索