Why is RabbitMQ not persisting messages on a durable queue?

后端 未结 2 1155
南旧
南旧 2021-02-03 10:59

I am using RabbitMQ with Django through Celery. I am using the most basic setup:

# RabbitMQ connection settings
BROKER_HOST = \'localhost\'
BROKER_PORT = \'5672\         


        
相关标签:
2条回答
  • 2021-02-03 11:38

    Making a queue durable is not the same as making the messages on it persistent. Durable queues mean they come up again automatically when the server has restarted - which has obviously happened in your case. But this doesn't affect the messages themselves.

    To make messages persistent, you have to also mark the message's delivery_mode property to 2. See the classic write-up Rabbits and Warrens for a full explanation.

    Edit: Full link is broken, but as of Dec 2013 you could still find the blog post from the main URL: http://blogs.digitar.com/jjww/

    0 讨论(0)
  • 2021-02-03 11:43

    To find out the messages delivery_mode you can consume it and look at the message properties:

    >>> from tasks import add
    >>> add.delay(2, 2)
    
    >>> from celery import current_app
    >>> conn = current_app.broker_connection()
    >>> consumer = current_app.amqp.get_task_consumer(conn)
    
    >>> messages = []
    >>> def callback(body, message):
    ...     messages.append(message)
    >>> consumer.register_callback(callback)
    >>> consumer.consume()
    
    >>> conn.drain_events(timeout=1)
    
    >>> messages[0].properties
    >>> messages[0].properties
    {'application_headers': {}, 'delivery_mode': 2, 'content_encoding': u'binary',    'content_type': u'application/x-python-serialize'}
    
    0 讨论(0)
提交回复
热议问题