pythonrabbitmq

How could I send a delayed message in rabbitmq using the rabbitmq-delayed-message-exchange plugin?


I have installed the plugin to send a delayed message from here rabbitmq-delayed-message-exchange.

I couldn't find any help for using it in python. I've just started using rabbitmq .

Here is what I've been trying:

import pika  
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare("test-x", type="x-delayed-message", arguments={"x-delayed-type":"direct"})  
channel.queue_declare(queue='task_queue',durable=True)
channel.queue_bind(queue="task_queue", exchange="test-x", routing_key="task_queue")
channel.basic_publish(exchange='test-x',routing_key='task_queue',body='Hello World! Delayed',arguments={"x-delay":100})
print(" [x] Sent 'Hello World! Delayed'")
connection.close()

Here are the exchanges listed:

sudo rabbitmqctl list_exchanges
Listing exchanges ...
amq.direct  direct
test-x  x-delayed-message
amq.fanout  fanout
amq.match   headers
amq.headers headers
    direct
amq.rabbitmq.trace  topic
amq.topic   topic
amq.rabbitmq.log    topic

I don't have a good idea how I could pass a delay argument to the basic_publish function

Any help is appreciated


Solution

  • You need to add the x-delay header to your message properties and specify the delay value in milliseconds. Try this:

    channel.basic_publish(
        exchange='test-x',
        routing_key='task_queue',
        body='Hello World! Delayed',
        properties=pika.BasicProperties(headers={"x-delay": 1000})
    )