// 消息发送
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++代码示例的更多相关文章

  1. rabbitmq AmqpClient 使用Topic 交换机投递与接收消息,C++代码示例

    // strUri = "amqp://guest:guest@192.168.30.11:8820/test" // strUri = "amqp://[帐户名]:[密 ...

  2. 7.RabbitMQ系列之topic主题交换器

    topic主题交换器它根据在队列绑定的路由键和路由模式通配符匹配将消息路由到队列. 生产者在消息头中添加路由键并将其发送到主题交换器. 收到消息后,exchange尝试将路由键与绑定到它的所有队列的绑 ...

  3. RabbitMQ基础学习笔记(C#代码示例)

    一.定义: MQ是MessageQueue,消息队列的简称(是流行的开源消息队列系统,利用erlang语言开发).MQ是一种应用程序对应用程序的通信方法.应用程序通过读写入队和出队的消息来通信,无需专 ...

  4. RabbitMQ的使用(五)RabbitMQ Java Client简单生产者、消费者代码示例

    pom文件: <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artif ...

  5. RabbitMQ 入门系列:9、扩展内容:死信队列:真不适合当延时队列。

    系列目录 RabbitMQ 入门系列:1.MQ的应用场景的选择与RabbitMQ安装. RabbitMQ 入门系列:2.基础含义:链接.通道.队列.交换机. RabbitMQ 入门系列:3.基础含义: ...

  6. RabbitMQ 入门系列:10、扩展内容:延时队列:延时队列插件及其有限的适用场景(系列大结局)。

    系列目录 RabbitMQ 入门系列:1.MQ的应用场景的选择与RabbitMQ安装. RabbitMQ 入门系列:2.基础含义:链接.通道.队列.交换机. RabbitMQ 入门系列:3.基础含义: ...

  7. rabbitmq AmqpClient 使用Direct 交换机投递与接收消息,C++代码示例

    // 以DIRECT 交换机和ROUTING_KEY的方式进行消息的发布与订阅 // send // strUri = "amqp://guest:guest@192.168.30.11:8 ...

  8. rabbitmq AmqpClient 使用Fanout 交换机投递与接收消息,C++代码示例

    fanout交换器重点内容非常简单.它只会将接收到的所有消息广播发送到它所知道的所有队列. 投递消息到交换机: #include "SimpleAmqpClient/SimpleAmqpCl ...

  9. RabbitMQ通过Exchange.topic 对routingkey 进行正则表达式匹配

    消费者: static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory() { HostNam ...

随机推荐

  1. PHP 与Python 读取大文件的区别

    php读取大文件的方法   <?php function readFile($file) { # 打开文件 $handle = fopen($file, 'rb'); while (feof($ ...

  2. linux100day(day6)--shell脚本简单逻辑

    if语句: if条件语句的使用格式: 1.单分支语句 if 条件;then 执行语句 fi 2.双分支语句 if 条件;then 执行语句1 else 执行语句2 fi 3.多分支语句 if 条件;t ...

  3. 2019HDU多校第一场 String 贪心

    题意:给你一个字符串,问是否存在一个长度为m的子序列,子序列中对应字符的数目必须在一个范围内,问是否存在这样的字符串?如果存在,输出字典序最小的那个. 思路:贪心,先构造一个序列自动机,序列自动机指向 ...

  4. eclipse maven install后查看报错信息

  5. securityDemo依赖

    <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit ...

  6. shell 输入输出重定向

    1. 命令列表: command > file 将输出重定向到file command < file 将输入重定向到file command >> file 将输出以追加的方式 ...

  7. mysql的安裝

    记得上学的时候,“研究”过一次mysql,找了篇文章,在课堂上念了.至今已经10余年,居然没再碰过数据库,自以为做嵌入式不用数据库,回头一看,却已经out许久... 上网下到最新的mysql5.5,从 ...

  8. POJ 1321 棋盘问题(dfs入门)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  9. CF gym 101933 K. King's Colors(二项式反演)

    传送门 解题思路 首先给出的树形态没用,因为除根结点外每个点只有一个父亲,它只需要保证和父亲颜色不同即可.设\(f(k)\)表示至多染了\(k\)种颜色的方案,那么\(f(k)=(k-1)^{(n-1 ...

  10. css 导航菜单+下拉菜单

    一.导航菜单 1.横向导航 代码如下: <!doctype html> <html> <head> <meta charset="utf-8&quo ...