简体   繁体   English

使用 ServiceBusMessageBatch 和在 TransactionScope 中发送多条消息有什么区别?

[英]What is the difference between using a ServiceBusMessageBatch and sending multiple messages in a TransactionScope?

Using Azure.Messaging.ServiceBus .使用Azure.Messaging.ServiceBus

I want to send multiple messages to Azure Service Bus in one transaction.我想在一次交易中向 Azure 服务总线发送多条消息。 I'm fine with the limit of 100 messages per transaction.我可以接受每笔交易 100 条消息的限制。

What are the pros and cons of these two approaches?这两种方法的优缺点是什么?

TransactionScope事务范围
Multiple single-message send operations in a TransactionScope. TransactionScope 中的多个单消息发送操作。

public async Task SendMessages(IEnumerable<ServiceBusMessage> messages, CancellationToken cancellationToken)
{
    using var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

    var tasks = messages.Select(m => _serviceBusSender.SendMessageAsync(m, cancellationToken));
    await Task.WhenAll(tasks);

    transaction.Complete();
}

ServiceBusMessageBatch ServiceBusMessageBatch
Single send operation, send a ServiceBusMessageBatch.单次发送操作,发送一个ServiceBusMessageBatch。

public async Task SendMessages(IEnumerable<ServiceBusMessage> messages, CancellationToken cancellationToken)
{
    var batch = await _serviceBusSender.CreateMessageBatchAsync(cancellationToken);

    foreach (var message in messages)
    {
        if (!batch.TryAddMessage(message))
            throw new Exception("...");
    }

    await _serviceBusSender.SendMessagesAsync(batch, cancellationToken);
}

A batch is a way to send multiple messages to the broker w/o exceeding the total size constraint as, during batch construction, the API will not add messages that would cause the batch size to exceed the maximum allowed.批处理是一种在不超过总大小限制的情况下向代理发送多条消息的方法,因为在批处理构建期间,API 不会添加会导致批处理大小超过允许的最大值的消息。 That's what a batch is for, for safety to ensure you don't get message size exception upon dispatching.这就是批处理的目的,为了安全以确保您在发送时不会收到消息大小异常。

TransactionScope ensures that messages sent within the same transaction are all sent if successful or reverted if one of the messages fails. TransactionScope确保在同一事务中发送的消息在成功时全部发送,或者在其中一条消息失败时还原。

With a batch, it's a single call to the broker.对于批处理,这是对代理的一次调用。 There are multiple concurrent calls to the broker with transaction scope, such as the number of send operations.有多个事务scope并发调用broker,比如send操作的次数。

The benefit of the batch: a single call to the broker that will have no chance to fail due to too many messages grouped together and exceeding maximum size.批处理的好处:对代理的单个调用不会因为太多消息组合在一起并超过最大大小而失败。 This is handy when you have multiple messages ready to be sent.当您准备发送多条消息时,这很方便。

The benefit of the transaction scope is when you need to dispatch messages but don't have them all available within the same instance or need to send in a fire-and-forget mode but still want to ensure those are sent atomically.事务 scope 的好处是当您需要分派消息但在同一实例中没有所有消息可用或需要以即发即弃模式发送但仍希望确保以原子方式发送这些消息时。

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

相关问题 FCM - 使用 API 发送消息/分钟的限制是什么? - FCM - What is the limitation of sending messages/minute using API? Twilio Whatsapp:发送多条媒体消息 - Twilio Whatsapp: Sending Multiple Media Messages 使用“Container.GetItemLinqQueryable”和“Container.GetItemQueryIterator”有什么区别? - What is the difference between using "Container.GetItemLinqQueryable" and "Container.GetItemQueryIterator"? getIdToken 和 getAppCheckToken 有什么区别? - What is difference between the getIdToken and getAppCheckToken? gcloud 和 gsutil 有什么区别? - What is the difference between gcloud and gsutil? 使用带有训练脚本的拥抱面估计器和直接在 AWS sagemaker 中使用笔记本有什么区别? - what is the difference between using a hugging face estimator with training script and directly using a notebook in AWS sagemaker? 关于数据节复制,使用数据导出服务和导出到数据湖有什么区别? - What's the difference between using Data Export Service and Export to Data Lake, regarding dataverse replication? Cloud Build 和 Cloud Deploy 有什么区别? - What is the difference between Cloud Build and Cloud Deploy? Firebase - ref 和 child 有什么区别? - Firebase - What is the difference between ref and child? 云服务中的存储和数据库有什么区别? - What is the difference between Storage and Databases in cloud services?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM