本文介绍了RabbitMQ 基础发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从rabbitmq的文档中的示例中用Python编写了RabbitMQ侦听器:

I have RabbitMQ listener written in Python from examples from rabbitmq's docs:

#!/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++ 客户端:

And C++ client which tries to send message:

#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);
}

但是当我发送消息时出现错误:

But when I sent message I got error:

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"); 成功发送.

But! When i delete line: channel->DeclareQueue("hound"); successfully sent.

用 Python 编写的发件人运行良好:

Sender writte in Python is working well:

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

What's wrong? Why c++ client show me this error?

推荐答案

此错误是由于您尝试 用不同的参数重新声明一个队列.

This error is caused by the fact that you are attempting to re-declare a queue with different parameters.

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

As the documentation states, a queue declaration is intended to be an idempotent assertion - if the queue does not exist, it is created. If it does exist, but has different parameters, you get this error.

声明和属性等效

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

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') 不同的事情.由于我们没有代码,因此无法进一步解释,但我认为这些信息足以让您解决问题.

Something is going on in your DeclareQueue("hound"); method that is different from channel.queue_declare(queue='hound'). Since we don't have the code for that, it is impossible to explain further, but I think this is sufficient information for you to solve the problem.

这篇关于RabbitMQ 基础发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:01