我已经简化了一个Rabbitmq使用者设置,试图确定当它们定向到的队列正在运行某些任务并且未向发布者发送确认消息时,新发布的消息的去向。 。接收方脚本在向发布方发送确认之前,会打个4声问候,间隔为10秒。据我了解,如果我使用命令sudo rabbitmqctl list_queues -p test2 messages
,我应该看到就绪消息和未确认消息的总和(源https://www.rabbitmq.com/rabbitmqctl.8.html)。假设我在5秒钟内运行了发件人脚本3次。我应该期望看到该命令的计数等于2,因为这3条消息之一已经发布。但是,我看到总数为0。这是发件人脚本。
import pika
credentials = pika.credentials.PlainCredentials("guest", "guest")
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.101.4', 5672, 'test2', credentials))
channel = connection.channel()
message = 'hello'
PERSISTENT_MESSAGES=2
channel.basic_publish(exchange='',
routing_key='tester',
body=message,
properties=pika.BasicProperties(
delivery_mode = PERSISTENT_MESSAGES, # make messages persistent
))
print 'sent options for tester'
connection.close()
这是我的接收方脚本
import pika
import time
credentials = pika.credentials.PlainCredentials("guest", "guest")
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.101.4', 5672, 'test2',
credentials))
channel = connection.channel()
channel.queue_declare(queue='tester',
durable=True,
arguments={'x-message-ttl': 1000,
"x-dead-letter-exchange": "dlx",
"x-dead-letter-routing-key": "dl",
'durable': True,
'x-max-length': 3})
def callback(ch, method, properties, body):
try:
print_hello()
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception as e:
print e
def print_hello():
print 'hello'
time.sleep(10)
print 'hello'
time.sleep(10)
print 'hello'
time.sleep(10)
print 'hello'
time.sleep(10)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(consumer_callback=callback, queue='tester')
channel.start_consuming()
在接收方脚本中,我将队列的最大长度设置为3,因此我知道该队列可以容纳我发送的所有消息。为什么总消息计数仅显示为0?这阻止了下一条消息和对print_hello()的调用被运行。另外,作为附带的问题,如果x-max-length
不存在,队列的大小是无限的还是只能处理一条消息?