gpt4 book ai didi

c# - RabbitMQ - 声明队列是什么意思?

转载 作者:行者123 更新时间:2023-11-30 15:54:02 25 4
gpt4 key购买 nike

我是 RabbitMQ 的新手,我有一个问题。这是我经常在教程中看到的示例片段。

    public static void Send(string queueName, string data)
{
using (IConnection connection = new ConnectionFactory().CreateConnection())
{
using (IModel channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queueName,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);

// Publish to the named queue
channel.BasicPublish(string.Empty, queue, null, Encoding.UTF8.GetBytes(data));
}
}
}

我很困惑,因为每次要调用 Send 函数时都声明一个队列的概念对我来说有点奇怪。这是否意味着它每次都创建一个新队列?

一些用于从队列接收的示例代码也有一个 queueDeclare 调用。为什么需要它?

最佳答案

不,它只在队列不存在时才创建队列:

Declare queue, create if needed.

This method creates or checks a queue. When creating a new queue the client can specify various properties that control the durability of the queue and its contents, and the level of sharing for the queue.

Declaration and Property Equivalence

Before a queue can be used it has to be declared. Declaring a queue will cause it to be created if it does not already exist. The declaration will have no effect if the queue does already exist and its attributes are the same as those in the declaration. When the existing queue attributes are not the same as those in the declaration a channel-level exception with code 406 (PRECONDITION_FAILED) will be raised.

引用文献 1 & 2

关于c# - RabbitMQ - 声明队列是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51425563/

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