meir dahan
meir dahan

Reputation: 103

RabbitMQ routing key does not route

i'm trying to do a simple message queue with RabitMQ i push a message with create_message

and then trying to get the message by the routing key.

it works great when the routing key is the same. the problem is when the routing key is different i keep on getting the message with the wrong routing key: for example

def callback(ch, method, properties, body):
    print("%r:%r" % (method.routing_key, body))

def create_message(self):
    connection = pika.BlockingConnection(pika.ConnectionParameters(
           'localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='www')
    channel.queue_declare(queue='hello')
    channel.basic_publish(exchange='www',
                  routing_key="11",
                  body='Hello World1111!')
    connection.close()
    self.get_analysis_task_celery()


def get_message(self):
    connection = pika.BlockingConnection(pika.ConnectionParameters(
           'localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='www')
    timeout = 1
    connection.add_timeout(timeout, on_timeout)
    channel.queue_bind(exchange="www", queue="hello", routing_key="10")
    channel.basic_consume(callback,
                  queue='hello',
                  no_ack=True,
                  consumer_tag= "11")

    channel.start_consuming()

example for my output: '11':'Hello World1111!'

what am i doing wrong?

tnx for the help

Upvotes: 1

Views: 3584

Answers (1)

Derick Bailey
Derick Bailey

Reputation: 72858

this is a total guess, since i can't see your rabbitmq server..

if you open the RabbitMQ management website and look at your exchange, you will probably see that the exchange is bound to the queue for routing key 10 and 11, both of which are bound to the same queue.

since both go to the same queue, your message will always be delivered to that queue, the consumer will always pick up the message

again, i'm guessing since i can't see your server. but check the server to make sure you don't have leftover / extra bindings

Upvotes: 1

Related Questions