为什么RabbitMQ不能在持久的队列上持久化邮件? [英] Why is RabbitMQ not persisting messages on a durable queue?
问题描述
#RabbitMQ连接设置
BROKER_HOST ='localhost'
BROKER_PORT ='5672'
BROKER_USER ='guest'
BROKER_PASSWORD ='guest'
BROKER_VHOST ='/'
我导入了一个芹菜任务,排队等待一年后运行。从iPython shell:
在[1]:from apps.test_app.tasks import add
在[2]中:dt = datetime.datetime(2012,2,18,10,00)
在[3]中:add.apply_async((10,6),eta = dt)
DEBUG:amqplib:从服务器,版本:8.0开始,属性:{u'information':'在MPL下许可。请参阅http://www.rabbitmq.com/',u'product':'RabbitMQ',u'version':'2.2.0',u'copyright':'版权所有(C)2007-2010 Lshift Ltd.,机构:['PLAIN','AMQPLAIN'],locales:['en_US']
DEBUG::hesive::::::::::::::: amqplib:打开OK! known_hosts []
DEBUG:amqplib:using channel_id:1
DEBUG:amqplib:channel open
DEBUG:amqplib:Closed channel#1
Out [3]:< AsyncResult: cfc507a1-175f-438E-ACEA-8c989a120ab3>
RabbitMQ在芹菜队列中收到此消息:
$ rabbitmqctl list_queues名字消息耐用
列表队列...
KTMacBook.local.celeryd.pidbox 0 false
芹菜1真
celeryctl_KTMacBook.local 0 true
...完成。然后,我通过击中控制C,然后按a中止,然后杀死了RabbitMQ。当我再次启动服务器并使用rabbitmqctl检查它时,它表示芹菜队列中没有消息: $ rabbitmqctl list_queues名称消息持久
列表队列...
celery 0 true
celeryctl_KTMacBook.local 0 true
... done。
芹菜队列经久耐用。为什么这些消息没有持续?
解决方案要查找消息 delivery_mode
您可以使用它,并查看消息属性:
>>>从任务导入添加
>>>> add.delay(2,2)
>>>从芹菜进口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)
>>>消息[0] .properties
>>>消息[0] .properties
{'application_headers':{},'delivery_mode':2,'content_encoding':u'binary','content_type':u'application / x-python-serialize'}
I am using RabbitMQ with Django through Celery. I am using the most basic setup:
# RabbitMQ connection settings
BROKER_HOST = 'localhost'
BROKER_PORT = '5672'
BROKER_USER = 'guest'
BROKER_PASSWORD = 'guest'
BROKER_VHOST = '/'
I imported a Celery task and queued it to run one year later. From the iPython shell:
In [1]: from apps.test_app.tasks import add
In [2]: dt=datetime.datetime(2012, 2, 18, 10, 00)
In [3]: add.apply_async((10, 6), eta=dt)
DEBUG:amqplib:Start from server, version: 8.0, properties: {u'information': 'Licensed under the MPL. See http://www.rabbitmq.com/', u'product': 'RabbitMQ', u'version': '2.2.0', u'copyright': 'Copyright (C) 2007-2010 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.', u'platform': 'Erlang/OTP'}, mechanisms: ['PLAIN', 'AMQPLAIN'], locales: ['en_US']
DEBUG:amqplib:Open OK! known_hosts []
DEBUG:amqplib:using channel_id: 1
DEBUG:amqplib:Channel open
DEBUG:amqplib:Closed channel #1
Out[3]: <AsyncResult: cfc507a1-175f-438e-acea-8c989a120ab3>
RabbitMQ received this message in the celery queue:
$ rabbitmqctl list_queues name messages durable
Listing queues ...
KTMacBook.local.celeryd.pidbox 0 false
celery 1 true
celeryctl_KTMacBook.local 0 true
...done.
I then killed RabbitMQ by hitting control-C followed by 'a' to abort. When I start the server again and check it with rabbitmqctl, it says that there are no messages in the celery queue:
$ rabbitmqctl list_queues name messages durable
Listing queues ...
celery 0 true
celeryctl_KTMacBook.local 0 true
...done.
The celery queue was durable. Why were the messages not persisted? What do I need to do to make the messages persistent?
解决方案 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'}
这篇关于为什么RabbitMQ不能在持久的队列上持久化邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!