c# - 如何从 Azure 服务总线主题订阅一次接收 N 条消息
问题描述
我有一个 Azure 服务总线主题订阅,其中消息不断增加。
下面的代码基本上是一次接收一条消息并处理它并将相关结果存储到数据库中。
我尝试设置 MaxConcurrentCalls to 10
,但由于数据库工作设计,它耗尽了我的数据库连接池。
所以我想一次从订阅中获取 10 条消息(接收一批 N 条消息)并希望使用一个数据库调用进行处理。
我没有看到任何批处理 api 选项,这可能吗?
我正在使用Microsoft.Azure.ServiceBus
nuget 版本4.1.1
。
_subscriptionClient = new SubscriptionClient(connectionString, topicName, subscriptionName);
// Register the callback method that will be invoked a message of interest is received
_subscriptionClient.RegisterMessageHandler(
async (message, token) =>
{
if (await ProcessMessage(message, token))
{
await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
}
},
new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false });
解决方案
预取使队列或订阅客户端能够在执行接收操作时从服务加载附加消息。
在此处检查接收批次: https ://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.subscriptionclient.receivebatch ?view=azure-dotnet
例子:
SubscriptionClient client = SubscriptionClient.CreateFromConnectionString(connectionString, topic, subName);
client.PrefetchCount = 10;
IEnumerable<BrokeredMessage> messageList = client.ReceiveBatch(5);
预取应大于或等于您希望从 ReceiveBatch 接收的消息数。
预取最多可以达到每秒处理的消息数的 n/3 倍,其中 n 是默认锁定持续时间。
推荐阅读
- php - 为什么需要在每个存储过程之前包含连接文件
- flutter - 如何使用文本字段中的位置动态过滤列表
- mysql - 在 Linux 中删除 MySQL BinLog
- .net - 从先前请求缓存的自定义 AuthorizeAttribute
- html - 居中文本和图像
- list - 如何插入颤振列表索引中的每隔 10 个元素?
- python - 如何使用 Python 脚本将文件上传到 ExpressJS 服务器?
- material-ui - 无法解析“@material-ui/core/icons/KeyboardArrowUp”
- c++ - 如何解决这些 cpp 错误?
- c# - 不应为已部署的应用程序启用开发环境