简体   繁体   English

接收来自天蓝色服务总线的消息

[英]Receiving messages from azure service bus

One can receive messages in azure service bus using either of the the two methods.. 一个人可以使用两种方法之一在azure服务总线中接收消息。

queueClient.BeginReceiveBatch OR messageReceiver.ReceiveBatchAsync queueClient.BeginReceiveBatch或messageReceiver.ReceiveBatchAsync

Is there any difference between these two methods speedwise or in any other way. 这两种方法在速度上或其他方面是否有任何区别。

Thanks 谢谢

If you don't need to the batch receive functionalilty, I prefer the method of wiring up a callback on the OnMessage event of the queue client. 如果不需要批处理接收功能,我更喜欢在队列客户端的OnMessage事件上连接回调的方法。 We have some fairly high throughput services relying on this pattern of message processing without any issues (1M+ messages/day) 我们有一些相当高吞吐量的服务依赖这种消息处理模式而没有任何问题(每天超过1M条消息)

I like that you end up with less, and simpler code, and can easily control the options of how many messages to process in parallel, which receive mode (peek and lock, vs receive and delete), etc 我希望您最终得到的代码更少,更简单,并且可以轻松控制并行处理多少条消息,接收模式(窥视和锁定,接收和删除)等选项。

There's a sample of it in this documentation : 本文档中有一个示例:

string connectionString =
  CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
QueueClient Client =
  QueueClient.CreateFromConnectionString(connectionString, "TestQueue");

// Configure the callback options
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

// Callback to handle received messages
Client.OnMessage((message) =>
{
    try
    {
        // Process message from queue
        Console.WriteLine("Body: " + message.GetBody<string>());
        Console.WriteLine("MessageID: " + message.MessageId);
        Console.WriteLine("Test Property: " +
        message.Properties["TestProperty"]);

        // Remove message from queue
        message.Complete();
    }
        catch (Exception)
    {
        // Indicates a problem, unlock message in queue
        message.Abandon();
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM