Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 285
Akshay Hazari Avatar asked Nov 14 '25 18:11

Akshay Hazari


1 Answers

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})
)
like image 69
Quentin Pradet Avatar answered Nov 17 '25 09:11

Quentin Pradet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!