Skip to content

Instantly share code, notes, and snippets.

@giacomelli
Last active August 15, 2019 19:29
Show Gist options
  • Save giacomelli/ed0d6eb6a8457eabed916e86c369c56c to your computer and use it in GitHub Desktop.
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
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