简体   繁体   中英

Pika worker throws exception when running channel.declare_queue

I'm writing a python client to accept job messages from a RabbitMQ broker and process the jobs, returning the results to another server. My script that sends messages to the RabbitMQ broker starts up fine, but my worker throws the following error when running channel.declare_queue(queue='task_queue')

pika.exceptions.AMQPChannelError: (406, "PRECONDITION_FAILED - parameters for queue 'task_queue' in vhost '/' not equivalent")

Client:

import pika    
connection = pika.BlockingConnection(pika.ConnectionParameters(host=cmdargs.server))
channel = connection.channel()
channel.queue_declare(queue='task_queue')
channel.basic_qos(prefetch_count=1)
channel.basic_consume(ProcJobCallback, queue='task_queue')
channel.start_consuming()

Server method that interacts with RabbitMQ:

def addNewJob(self, newJob):
        self.jobList.append(newJob)
        connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        channel = connection.channel()
        channel.queue_declare(queue='task_queue')

        for tile in newJob.TileStatus:
                message = "{0},{1},{2}".format(newJob, tile[0], tile[1])
                channel.basic_publish(exchange='', routing_key='task_queue', body=message, properties=pika.BasicProperties(delivery_mode = 2, ))
        connection.close()

Any help or insight is greatly appreciated.

EDIT: I discovered why I was getting an error with the code listed above. I was specifying delivery_mode=2 when publishing my messages, but when I declared the queue, I forgot to add the Durable=True parameter.

Are you sure you are connecting to the same server (host) on the publisher and consumer side?

connection = pika.BlockingConnection(pika.ConnectionParameters(host=cmdargs.server))

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))

如果你的队列是持久的,只需删除声明“channel.queue_declare(queue ='task_queue')”,这在你的情况下就足够了。

i meet the same problem when I try to make the queue msg persistent with durable=True.

Try to rename the queue name, it works well with my script. Maybe kill the queue new_task , and re-run your script also works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM