Last active
August 15, 2019 19:29
-
-
Save giacomelli/ed0d6eb6a8457eabed916e86c369c56c to your computer and use it in GitHub Desktop.
Can I send batch messages larger than 256 Kb to Azure Service Bus? http://diegogiacomelli.com.br/can-i-send-batch-messages-larger-than-256-kb-to-azure-service-bus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.ServiceBus.Messaging; | |
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Sample | |
{ | |
public class AzureServiceBus<TMessage> | |
{ | |
private readonly QueueClient _client; | |
public AzureServiceBus(string connectionString, string queueName) | |
{ | |
_client = QueueClient.CreateFromConnectionString(connectionString, queueName); | |
} | |
public async Task SendBatchAsync(IEnumerable<TMessage> messages) | |
{ | |
var messagesToSend = messages | |
.Select(x => JsonConvert.SerializeObject(x)) | |
.ToList(); | |
// There is a 256 KB limit per message sent on Azure Service Bus. | |
// We will divide it into messages block lower or equal to 256 KB. | |
// Maximum message size: 256 KB for Standard tier, 1 MB for Premium tier. | |
// Maximum header size: 64 KB. | |
// https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-quotas | |
var maxBatchSize = 256000 - 64000; | |
var pagedMessages = new List<BrokeredMessage>(); | |
while (messagesToSend.Count > 0) | |
{ | |
var pageSize = 0L; | |
foreach(var msg in messagesToSend) | |
{ | |
var brokeredMessage = new BrokeredMessage(msg); | |
if (pageSize + brokeredMessage.Size > maxBatchSize) | |
break; | |
pageSize += brokeredMessage.Size; | |
pagedMessages.Add(brokeredMessage); | |
} | |
try | |
{ | |
await _client.SendBatchAsync(pagedMessages); | |
} | |
catch(MessageSizeExceededException) | |
{ | |
// Due to system overhead, this limit is less than these values. | |
// https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-quotas | |
maxBatchSize -= 10000; | |
continue; | |
} | |
messagesToSend.RemoveRange(0, pagedMessages.Count); | |
pagedMessages.Clear(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment