我正在尝试通过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
中的第三个布尔参数)。另外,我的测试用户具有完全特权。这可能是什么问题?谢谢。
答案 0 :(得分:0)
好吧,我发现BasicConsume
的默认值为exclusive
,因此在声明您的消费者时,使其为false
很重要,如下所示(第三个布尔参数) :
std::string consumer = connection->BasicConsume("rabbit", "", true, false, false);
^^^^^
有趣的是,我在官方文档中没有找到关于此的任何句子。