【EasyNetQ】- 发布确认

默认的AMQP发布不是事务性的,并不保证您的消息实际到达代理。AMQP确实指定了事务发布,但是使用RabbitMQ它非常慢,我们还没有通过EasyNetQ API支持它。对于高性能保证交付,建议您使用“发布者确认”。简单来说,这是AMQP的扩展,它在代理成功接收到您的消息时提供回调。

“成功收到”是什么意思?这取决于 ...

  • 瞬时消息在入队时确认。
  • 只要持久性消息持久保存到磁盘,或者在每个队列上消耗持久消息,就会确认该消息。
  • 直接确认不可路由的瞬态消息并将其发布。

有关发布者确认的更多信息,请阅读RabbitMQ博客上的公告

通过在连接字符串上设置publisherConfirms = true来启用发布者确认:

bus = RabbitHutch.CreateBus("host=localhost;publisherConfirms=true;timeout=10");

 

同步bus.Publish(..)方法将在返回之前等待确认。在超时期限之前未确认(也在连接字符串中配置)将导致抛出异常。随着发布者确认同步发布方法将显着减慢。如果需要考虑性能,则应考虑使用PublishAsync方法:

bus.PublishAsync(new MyMessage
    {
        Text = "Hello World"
    }).ContinueWith(task =>
        {
            // this only checks that the task finished
            // IsCompleted will be true even for tasks in a faulted state
            // we use if (task.IsCompleted && !task.IsFaulted) to check for success
            if (task.IsCompleted) 
            {
                //Console.Out.WriteLine("{0} Completed", count);
            }
            if (task.IsFaulted)
            {
                Console.Out.WriteLine("\n\n");
                Console.Out.WriteLine(task.Exception);
                Console.Out.WriteLine("\n\n");
            }
        });

 

这将在收到确认之前返回。如果未收到确认或NACK确认,则任务将在故障状态下完成。

 

posted @ 2018-08-07 15:48  wangwust  阅读(383)  评论(0编辑  收藏  举报