我有一个用Python语言编写的RabbitMQ监听器,来自rabbitmq文档的例子:
#!/usr/bin/env python
import time
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hound')
def callback(ch, method, properties, body):
print(" [x] Received %r" % (body,))
time.sleep(5)
print(" [x] Done")
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_consume(callback,
queue='hound',
)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()以及尝试发送消息的C++客户端:
#include <SimpleAmqpClient/SimpleAmqpClient.h>
using namespace AmqpClient;
int main(int argc, char *argv[])
{
Channel::ptr_t channel;
channel = Channel::Create("SERVER_HOST", SERVER_PORT,
"LOGIN", "PASS", "/");
BasicMessage::ptr_t msg = BasicMessage::Create("HELLO!!!");
channel->DeclareQueue("hound");
channel->BasicPublish("", "hound", msg, true);
}但是当我发送消息时,我得到了错误:
terminate called after throwing an instance of 'AmqpClient::PreconditionFailedException'
what(): channel error: 406: AMQP_QUEUE_DECLARE_METHOD caused: PRECONDITION_FAILED - parameters for queue 'hound' in vhost '/' not equivalent
Aborted但!当我删除行:channel->DeclareQueue("hound");成功发送。
用Python编写的发送器运行良好:
#!/usr/bin/env python
import sys
import pika
credentials = pika.PlainCredentials(
username=username, password=password
)
connection = pika.BlockingConnection(
pika.ConnectionParameters(
host=host,
virtual_host=virtual_host,
credentials=credentials,
port=RABBIT_PORT
)
)
channel = connection.channel()
channel.queue_declare(queue='hound')
channel.basic_publish(exchange='',
routing_key='hound',
body='hello!')
print(" [x] Sent %r" % (message,))怎么了?为什么c++客户端显示此错误?
发布于 2018-01-31 00:55:25
此错误是由您正在尝试re-declare a queue with different parameters的事实引起的。
正如文档所述,队列声明应该是一个idempotent assertion -如果队列不存在,就会创建它。如果它确实存在,但具有不同的参数,则会出现此错误。
声明和属性等价
在使用队列之前,必须先声明它。如果队列尚不存在,则声明队列将导致创建该队列。如果队列已经存在,并且其属性与声明中的属性相同,则声明将不起作用。当现有的队列属性与声明中的属性不同时,将引发通道级别的异常,代码为406 (PRECONDITION_FAILED)。
您的DeclareQueue("hound");方法中发生了一些与channel.queue_declare(queue='hound')不同的事情。因为我们没有代码,所以不可能进一步解释,但我认为这是解决问题的足够信息。
https://stackoverflow.com/questions/48523425
复制相似问题