How to create a delayed queue in RabbitMQ

suggest change

First we need to set up two basic channels, one for the main queue, and one for the delay queue. In my example at the end, I include a couple of additional flags that are not required, but make the code more reliable; such as confirm_deliveries, delivery_mode and durable. You can find more information on these in the RabbitMQ manual.

After we have set up the channels, we add a binding to the main channel that we can use to send messages from the delay channel to our main queue.

channel.queue.bind(exchange='amq.direct', routing_key='hello', queue='hello')

Next we need to configure our delay channel to forward messages to the main queue once they have expired.

delay_channel.queue.declare(queue='hello_delay', durable=True, arguments={
    'x-message-ttl': 5000,
    'x-dead-letter-exchange': 'amq.direct',
    'x-dead-letter-routing-key': 'hello'
})

This sets, in milliseconds, how long a message stays in the delay queue before it expires. With the two dead-letter arguments below, expiry transfers the message instead of removing it.

This is the routing key used to transfer the message to another queue once it has expired, instead of the default behaviour of removing it.

This variable determines which exchange is used to transfer the message from the hello_delay queue to the hello queue.

Publishing to the delay queue

When the AMQPStorm channel is set up, send a message to the delay queue with basic.publish.

delay_channel.basic.publish(exchange='',
                            routing_key='hello_delay',
                            body='test',
                            properties={'delivery_mode': 2})

Once you have executed the script, you should see the following queues in the RabbitMQ management interface.

Example.

from amqpstorm import Connection

connection = Connection('127.0.0.1', 'guest', 'guest')

# Create normal 'Hello World' type channel.
channel = connection.channel()
channel.confirm_deliveries()
channel.queue.declare(queue='hello', durable=True)

# We need to bind this channel to an exchange, that will be used to transfer
# messages from our delay queue.
channel.queue.bind(exchange='amq.direct', routing_key='hello', queue='hello')

# Create our delay channel.
delay_channel = connection.channel()
delay_channel.confirm_deliveries()

# This is where we declare the delay, and routing for our delay channel.
delay_channel.queue.declare(queue='hello_delay', durable=True, arguments={
    'x-message-ttl': 5000, # Delay until the message is transferred in milliseconds.
    'x-dead-letter-exchange': 'amq.direct', # Exchange used to transfer the message from A to B.
    'x-dead-letter-routing-key': 'hello' # Routing key of the queue we want the message transferred to.
})

delay_channel.basic.publish(exchange='',
                            routing_key='hello_delay',
                            body='test',
                            properties={'delivery_mode': 2})

print("[x] Sent")

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents