python - RabbitMQ 基础发布

标签 python c++ rabbitmq

我有从 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->DeclareQueue("hound"); 成功发送。

用 Python 编写的 Sender 运行良好:

#!/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++ 客户端向我显示此错误?

最佳答案

此错误是由于您试图 re-declare a queue with different parameters .

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

Declaration and Property Equivalence

Before a queue can be used it has to be declared. Declaring a queue will cause it to be created if it does not already exist. The declaration will have no effect if the queue does already exist and its attributes are the same as those in the declaration. When the existing queue attributes are not the same as those in the declaration a channel-level exception with code 406 (PRECONDITION_FAILED) will be raised.

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

关于python - RabbitMQ 基础发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48523425/

相关文章:

python - Wagtail - 可以在迁移中重命名流场中的项目吗?

python - XOR 运算符没有给出预期的结果?

python - Django ORM : Dynamically determine another table name according one field value

python - 使用 boost::numpy::ndarray 时出现段错误

c++ - 如何在 borland c++ 5.02 中使用 ROM BIOS 中断?

java - RabbitMQ 订阅者超时

ruby-on-rails - Heroku 上的延迟作业与 RabbitMQ 有何优缺点?

c++ - 在 Windows 上构建 MongoDB C++ 驱动程序

c++ - Pthreaded 程序在 Linux 机器上导致段错误。在 Cygwin64 上运行良好

java - SpringBoot + RabbitMQ 抛出错误 : java.net.ConnectException: Connection refused