gpt4 book ai didi

rabbitmq - 为什么 RabbitMQ 中的消息优先级不对我的消息进行排序?

转载 作者:行者123 更新时间:2023-12-02 20:19:14 25 4
gpt4 key购买 nike

我与生产者和消费者一起运行 Elixir 程序。我为我的消息设置了优先级。但优先级不起作用。

我的程序:

发布者:

Basic.publish(channel, "my_exchange", "my_queue", "1", persistent: true, priority: 2)
Basic.publish(channel, "my_exchange", "my_queue", "2", persistent: true, priority: 3)
Basic.publish(channel, "my_exchange", "my_queue", "3", persistent: true, priority: 1)
Basic.publish(channel, "my_exchange", "my_queue", "4", persistent: true, priority: 0)
Basic.publish(channel, "my_exchange", "my_queue", "5", persistent: true, priority: 4)

消费者:

def init(_) do
Connection.open(Application.get_env(:app, :rabbitmq))
{:ok, channel} = Channel.open(conn)
:ok = Basic.qos(channel, prefetch_count: 1, global: true)
{:ok, _consumer_tag} = Basic.consume(channel, "my_queue", nil, arguments: [{"x-priority", :signedint, 100}])
{:ok, %{channel: channel}}
end

def handle_info({:basic_deliver, payload, %{delivery_tag: tag, priority: priority}}, %{channel: channel} = state) do

Logger.info("Received message with payload: #{payload} and priority: #{priority}")

Basic.ack(channel, tag)

{:noreply, state}
end

发布后,我运行消费者。

预期输出:

Received message with payload: 5 and priority: 4
Received message with payload: 2 and priority: 3
Received message with payload: 1 and priority: 2
Received message with payload: 3 and priority: 1
Received message with payload: 4 and priority: 0

实际输出:

Received message with payload: 1 and priority: 2
Received message with payload: 2 and priority: 3
Received message with payload: 3 and priority: 1
Received message with payload: 4 and priority: 0
Received message with payload: 5 and priority: 4

我做错了什么吗?消息优先级不起作用吗?

最佳答案

人们不应该期望消息在队列中排序,因为“队列”的基本含义(大约任何队列要么是 LIFO 要么 FIFO ,RabbitMQ 的队列是后者。)

对队列中的消息进行打乱,以便在每次插入时按优先级对它们进行排序,这会大大降低性能。 RabbitMQ(自 3.5.0 起有效)实际允许的是:

注意尽管如此,从 3.5.0 开始,RabbitMQ 允许对队列中卡住的消息进行排序,假设消费者得到消息进入队列的速度较慢。在这种情况下,未使用的消息将被排序。

尽管如此,它仍然无法保证排序。优先级只允许您让某些队列/消费者“举手”首先接收消息除非它们被阻止。否则,优先级链中的下一个将收到消息。

如果您需要对收入进行排序(很难想象为什么在这种情况下您会选择消息队列,但仍然如此),您必须在发送到发布者中的队列之前对其进行排序 em>,或者从消费者的队列收集到所有预期的消息之后。

关于rabbitmq - 为什么 RabbitMQ 中的消息优先级不对我的消息进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51811823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com