簡體   English   中英

RabbitMQ / Pika-確保以創建的順序接收消息?

[英]RabbitMQ / Pika - guaranteeing that messages are received in the order created?

作為一個簡單的示例,我將5個項目添加到新的RabbitMQ(v 2.6.1)隊列中:

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='not.my.real.server.net'))
channel = connection.channel()
channel.queue_declare(queue='dw.neil',durable=True)
# add 5 messages to the queue, the numbers 1-5
for x in range(5):
    message = x+1
    channel.basic_publish(exchange='',routing_key='dw.neil', body=str(message))
    print " [x] Sent '%s'" % message
connection.close()

我清除隊列,然后運行上面的代碼以添加5個項目:

nkodner@hadoop4 sports_load_v2$ python send_5.py 
 [x] Sent '1'
 [x] Sent '2'
 [x] Sent '3'
 [x] Sent '4'
 [x] Sent '5'

現在,我正在嘗試模擬失敗的處理。 給定以下要從隊列中使用的代碼。 請注意,我對basic_ack的調用已被注釋掉:

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='not.my.real.server.net'))
channel = connection.channel()
channel.queue_declare(queue='dw.neil',durable=True)
method_frame, header_frame, body=channel.basic_get(queue='dw.neil')
print method_frame, header_frame
print "body: %s" % body
#channel.basic_ack(delivery_tag=method_frame.delivery_tag)
connection.close()

我運行接收代碼以搶占隊列中的項目。 如我所料,我得到了項目1:

nkodner@hadoop4 sports_load_v2$ python r5.py 
<Basic.GetOk(['message_count=9', 'redelivered=False', 'routing_key=dw.neil', 'delivery_tag=1', 'exchange='])>
<BasicProperties([])>
body: 1

由於對channel.basic_ack()的調用已被注釋掉,因此我希望將未確認的消息放入隊列中,以便下一個使用者使用它。 我希望消息#1是隊列之外的第一條消息,並且Redelivered屬性設置為True。 而是收到消息#2:

nkodner@hadoop4 sports_load_v2$ python r5.py 
<Basic.GetOk(['message_count=9', 'redelivered=False', 'routing_key=dw.neil', 'delivery_tag=1', 'exchange='])>
<BasicProperties([])>
body: 2

並且#1回來之前,隊列中的所有其他消息都已收到,並且Redelivered標志設置為True:

...

nkodner@hadoop4 sports_load_v2$ python r5.py 
<Basic.GetOk(['message_count=9', 'redelivered=False', 'routing_key=dw.neil', 'delivery_tag=1', 'exchange='])>
<BasicProperties([])>
body: 5

nkodner@hadoop4 sports_load_v2$ python r5.py 
<Basic.GetOk(['message_count=9', 'redelivered=True', 'routing_key=dw.neil', 'delivery_tag=1', 'exchange='])>
<BasicProperties([])>
body: 1

我是否可以設置任何屬性或選項,以便在得到確認之前一直交付#1?

我的用例是使用順序生成的文件加載數據倉庫。 我們正在使用基於消息的處理,以使我的程序知道一些新文件已經准備好並將被加載到DW中。 我們必須按照文件生成的順序進行處理。

這已在RabbitMQ 2.7.0中得到解決-我們正在運行2.6.1。

發行說明中

此版本中的新功能包括:

  • 為消費者重新排隊的消息保留順序

嘗試使用channel.basic_reject-這會將未確認的消息推回RabbitMQ,RabbitMQ會將消息視為新消息。 另外-如果您有失敗的消息被阻塞,則可以使用channel.basic_recover告訴RabbitMQ重新傳遞所有未確認的消息。

http://www.rabbitmq.com/extensions.html#negative-acknowledgements提供了有關Basic.Reject與Basic.Nack的區別信息。

消息排序語義在http://www.rabbitmq.com/semantics.html上進行了解釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM