使用 ServiceBusMessageBatch 和在 TransactionScope 中发送多条消息有什么区别?
使用Azure.Messaging.ServiceBus。
我想在一个事务中向 Azure 服务总线发送多条消息。我同意每笔交易 100 条消息的限制。
这两种方法的优缺点是什么?
交易范围
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。
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);
}
Using Azure.Messaging.ServiceBus
.
I want to send multiple messages to Azure Service Bus in one transaction. I'm fine with the limit of 100 messages per transaction.
What are the pros and cons of these two approaches?
TransactionScope
Multiple single-message send operations in a 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
Single send operation, send a 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
批处理是一种在不超过总大小限制的情况下向代理发送多条消息的方法,因为在批处理构建期间,API 不会添加会导致批处理大小超过允许的最大值的消息。这就是批处理的用途,为了安全起见,确保您在分派时不会出现消息大小异常。
TransactionScope
确保同一事务中发送的消息在成功时全部发送,或者在其中一条消息失败时恢复。对于批量,只需对代理进行一次调用即可。有多个对具有事务范围的代理的并发调用,例如发送操作的数量。
批处理的好处:对代理的单次调用不会因为太多消息组合在一起并超过最大大小而失败。当您准备发送多条消息时,这非常方便。
事务作用域的好处是,当您需要分派消息但在同一实例中无法将它们全部提供,或者需要以“即发即弃”模式发送但仍希望确保这些消息以原子方式发送时。
A batch is a way to send multiple messages to the broker without 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. 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.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.
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.