gpt4 book ai didi

c# - RabbitMQ C# 验证消息已发送

转载 作者:太空狗 更新时间:2023-10-30 00:51:58 25 4
gpt4 key购买 nike

我是 RabbitMQ 的新手,正在尝试写入队列并验证消息是否已发送。如果它失败了,我需要知道它。我做了一个假队列来观察它失败,但无论我看到什么,都没有异常,当我寻找一个 ack 时,我总是得到一个。我从未见过 BasicNack。

我什至不确定 BasicAcks 是否可行。

    private void button1_Click(object sender, EventArgs e)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("task_queue", true, false, false, null);

var message = ("Helllo world");
var body = Encoding.UTF8.GetBytes(message);
channel.ConfirmSelect();

var properties = channel.CreateBasicProperties();
properties.SetPersistent(true);
properties.DeliveryMode = 2;
channel.BasicAcks += channel_BasicAcks;
channel.BasicNacks += channel_BasicNacks;
//fake queue should be task_queue
channel.BasicPublish("", "task_2queue", true, properties, body);

channel.WaitForConfirmsOrDie();

Console.WriteLine(" [x] Sent {0}", message);
}
}
}

void channel_BasicNacks(IModel model, BasicNackEventArgs args)
{

}

void channel_BasicAcks(IModel model, BasicAckEventArgs args)
{

}

最佳答案

对于那些正在寻找 C# 答案的人 - 这就是您所需要的。

https://rianjs.net/2013/12/publisher-confirms-with-rabbitmq-and-c-sharp

像这样:(BasicAcks 附加了一个事件处理程序——还有 BasicNacks)

using (var connection = FACTORY.CreateConnection())
{
var channel = connection.CreateModel();
channel.ExchangeDeclare(QUEUE_NAME, ExchangeType.Fanout, true);
channel.QueueDeclare(QUEUE_NAME, true, false, false, null);
channel.QueueBind(QUEUE_NAME, QUEUE_NAME, String.Empty, new Dictionary<string, object>());
channel.BasicAcks += (sender, eventArgs) =>
{
//implement ack handle
};
channel.ConfirmSelect();

for (var i = 1; i <= numberOfMessages; i++)
{
var messageProperties = channel.CreateBasicProperties();
messageProperties.SetPersistent(true);

var message = String.Format("{0}\thello world", i);
var payload = Encoding.Unicode.GetBytes(message);
Console.WriteLine("Sending message: " + message);
channel.BasicPublish(QUEUE_NAME, QUEUE_NAME, messageProperties, payload);
channel.WaitForConfirmsOrDie();
}
}

关于c# - RabbitMQ C# 验证消息已发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24151705/

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