【问题标题】:RabbitMQ basic publishRabbitMQ 基本发布
【发布时间】:2018-01-30 14:09:40
【问题描述】:

我有来自 rabbitmq 文档中的示例用 Python 编写的 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-&gt;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++ 客户端显示这个错误?

【问题讨论】:

    标签: python c++ rabbitmq


    【解决方案1】:

    此错误是由于您尝试re-declare a queue with different parameters 造成的。

    正如文档所述,队列声明旨在成为idempotent assertion - 如果队列不存在,则会创建它。如果它确实存在,但具有不同的参数,则会出现此错误。

    声明和属性等价

    在使用队列之前,必须先声明它。声明一个队列 如果它尚不存在,将导致它被创建。这 如果队列已经存在并且声明将无效 它的属性与声明中的相同。当。。。的时候 现有队列属性与声明中的不同 代码为 406 (PRECONDITION_FAILED) 的通道级异常将是 提出来。

    您的DeclareQueue("hound"); 方法中发生了与channel.queue_declare(queue='hound') 不同的事情。由于我们没有这方面的代码,因此无法进一步解释,但我认为这些信息足以让您解决问题。

    【讨论】:

    • 所以我已经解决了promleb。这是因为 python 的 pika 客户端中的所有参数都是错误的,但在 c++ 中最后两个参数是正确的。当我在 C++ 客户端中更改为: channel->DeclareQueue("hound", false, false, false, false);错误消失了。
    猜你喜欢
    • 1970-01-01
    • 2014-01-24
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多