gpt4 book ai didi

python - RabbitMQ 基础发布

转载 作者:行者123 更新时间:2023-11-28 01:37:51 25 4
gpt4 key购买 nike

我有从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com