RabbitMQ基本发布

2024-03-29 15:00:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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++客户端尝试发送消息:

^{pr2}$

但当我发信息时,我收到了一个错误:

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 write运行良好:

#!/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++客户端显示这个错误?在


Tags: importhostbinbasicqueueusrchannelrabbitmq
1条回答
网友
1楼 · 发布于 2024-03-29 15:00:11

此错误是由于您试图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')不同的问题。由于我们没有相关的代码,因此不可能进一步解释,但我认为这些信息足以帮助您解决问题。在

相关问题 更多 >