简体   繁体   English

服务总线队列的性能问题

[英]Performance issue on Service bus queue

I have huge amount of messages per second sent to my Service bus queue.我每秒有大量消息发送到我的服务总线队列。 I am using below code to send messages:我正在使用以下代码发送消息:

var message = new Message(body);

var result = await queueClient.SendAsync(messageOne)

I am facing performance issues, is there anything I can do to optimize the code?我面临性能问题,有什么可以优化代码的吗?

PS - I have newly joined my company and new to Azure. PS - 我刚加入我的公司并且刚接触 Azure。

You can refer to this documentation for optimising your code.你可以参考这个文档来优化你的代码。

Performing an operation (send, receive, delete, etc.) takes some time.执行操作(发送、接收、删除等)需要一些时间。 This time includes the processing of the operation by the Service Bus service in addition to the latency of the request and the response.除了请求和响应的延迟外,此时间还包括服务总线服务对操作的处理。 To increase the number of operations per time, operations must execute concurrently.要增加每次操作的数量,操作必须并发执行。

The client schedules concurrent operations by performing asynchronous operations.客户端通过执行异步操作来调度并发操作。 The next request is started before the previous request is completed.下一个请求在前一个请求完成之前开始。 The following code snippet is an example of an asynchronous send operation:以下代码片段是异步发送操作的示例:

var messageOne = new Message(body);
var messageTwo = new Message(body);

var sendFirstMessageTask =
    queueClient.SendAsync(messageOne).ContinueWith(_ =>
    {
        Console.WriteLine("Sent message #1");
    });
var sendSecondMessageTask =
    queueClient.SendAsync(messageTwo).ContinueWith(_ =>
    {
        Console.WriteLine("Sent message #2");
    });

await Task.WhenAll(sendFirstMessageTask, sendSecondMessageTask);
Console.WriteLine("All messages sent");

The following code is an example of an asynchronous receive operation.以下代码是异步接收操作的示例。

var receiver = new MessageReceiver(connectionString, queueName, ReceiveMode.PeekLock);

static Task LogErrorAsync(Exception exception)
{
    Console.WriteLine(exception);
    return Task.CompletedTask;
};

receiver.RegisterMessageHandler(
    async (message, cancellationToken) =>
    {
        Console.WriteLine("Handle message");
        await receiver.CompleteAsync(message.SystemProperties.LockToken);
    },
    new MessageHandlerOptions(e => LogErrorAsync(e.Exception))
    {
        AutoComplete = false,
        MaxConcurrentCalls = 20
    });

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

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