rabbitmq - RabbitMQ:ACCESS_REFUSED 即使队列是非独占的
问题描述
我正在尝试通过 C++ 中的 RabbitMQ 在两台计算机之间发送和接收消息。我的发件人代码如下所示:
AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Create("192.168.1.1", 5672, "test", "test", "/");
channel->DeclareQueue("rabbit", false, true, false, false);
AmqpClient::BasicMessage::ptr_t msg = AmqpClient::BasicMessage::Create("Hello Rabbit!");
channel->BasicPublish("", "rabbit", msg);
std::cout << "[x] message sent!" << std::endl;
我在另一台具有 ip 地址的计算机上的接收器代码192.168.1.1
如下所示:
AmqpClient::Channel::ptr_t connection = AmqpClient::Channel::Create("localhost", 5672, "test", "test", "/");
connection->DeclareQueue("rabbit", false, true, false, false);
std::string consumer = connection->BasicConsume("rabbit", "", true, false);
while(true)
{
AmqpClient::Envelope::ptr_t envelope = connection->BasicConsumeMessage(consumer);
std::cout << "received message: " << envelope->Message()->Body() << std::endl;
std::cout << "[x] ready to receive new message..." << std::endl;
connection->BasicAck(envelope);
}
当接收方在发送方代码完成并终止后开始接收时,这工作得很好。但是当我第一次打开接收器让它待机并发送消息时,在发送方,我收到以下错误:
terminate called after throwing an instance of 'AmqpClient::AccessRefusedException'
what(): channel error: 403: AMQP_BASIC_CONSUME_METHOD caused: ACCESS_REFUSED - queue 'rabbit' in vhost '/' in exclusive use
我知道当声明的队列处于独占模式时应该引发此错误,但在这里我将其声明为非独占(中的第三个布尔参数channel->DeclareQueue
)。此外,我的测试用户拥有完全权限。这里可能是什么问题?谢谢你。
解决方案
好的,我发现 is 的默认值BasicConsume
,exclusive
所以在声明你的消费者时让它是很重要的false
(第三个布尔参数):
std::string consumer = connection->BasicConsume("rabbit", "", true, false, false);
^^^^^
有趣的是,我在官方文档中没有找到任何关于此的句子。
推荐阅读
- r - 用线或箭头将图例链接到绘图
- javascript - 如何通过 expressjs 访问 req.body?
- javascript - 由于未知的 bcrypt 问题,登录时出错
- node.js - nodejs,级联调用异步函数,直到条件发生
- .net - 如何显示创建项目的 dotnet 新模板版本?
- php - 需要帮忙。准备好的信封已注册但未发送到电子邮件
- sql - 执行具有 case 语句和连接的选择查询时出错
- c# - ASP .NET 将 DateTime 值从视图传递到控制器
- javascript - React:获取输入的状态
- java - Forge 1.12.2 Coremodding:java.lang.StringIndexOutOfBoundsException