Skip to content

Commit e3b2ef6

Browse files
Cap ServiceBusMessageBatch size at 1 MB to match broker enforcement
The Service Bus broker enforces a 1 MB batch size limit regardless of the max-message-size advertised on the AMQP link. Premium partitioned namespaces advertise 100 MB on the link, causing tryAddMessage() to accept batches the broker will reject. Cap batch creation in ServiceBusSenderAsyncClient.createMessageBatch() at 1 MB (MAX_BATCH_SIZE_BYTES). This is the single enforcement point: both sendMessages(iterable) and scheduleMessages(iterable) call createMessageBatch internally. Single-message paths (sendMessage, scheduleMessage) are NOT capped since the 1 MB limit is batch-specific and individual messages on Premium can validly exceed 1 MB up to the per-entity limit. When a user requests a batch size exceeding 1 MB via CreateMessageBatchOptions, throw ServiceBusException. Tracking: azure-service-bus#708 ICM: 51000000793879
1 parent 66f5537 commit e3b2ef6

2 files changed

Lines changed: 343 additions & 4 deletions

File tree

sdk/servicebus/azure-messaging-servicebus/src/main/java/com/azure/messaging/servicebus/ServiceBusSenderAsyncClient.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ public final class ServiceBusSenderAsyncClient implements AutoCloseable {
222222
* The default maximum allowable size, in bytes, for a batch to be sent.
223223
*/
224224
static final int MAX_MESSAGE_LENGTH_BYTES = 256 * 1024;
225+
// Temporary workaround: Service Bus enforces a maximum batch payload size of 1 MB that is not
226+
// communicated via the AMQP link's max-message-size property. The link reports the per-message
227+
// limit (up to 100 MB for Premium partitioned), but the broker rejects batch sends above 1 MB.
228+
// This cap is applied only in createMessageBatch(), which is the single enforcement point for
229+
// batch size limits. The sendMessages(iterable) and scheduleMessages(iterable) paths use
230+
// createMessageBatch() internally and are therefore also capped. Single-message paths
231+
// (sendMessage, scheduleMessage) are not capped since individual messages on Premium can
232+
// validly exceed 1 MB up to the per-entity limit.
233+
// Tracked by: https://github.com/Azure/azure-service-bus/issues/708
234+
static final int MAX_BATCH_SIZE_BYTES = 1024 * 1024;
225235
private static final String TRANSACTION_LINK_NAME = "coordinator";
226236
private static final ServiceBusMessage END = new ServiceBusMessage(new byte[0]);
227237
private static final CreateMessageBatchOptions DEFAULT_BATCH_OPTIONS = new CreateMessageBatchOptions();
@@ -463,15 +473,15 @@ public Mono<ServiceBusMessageBatch> createMessageBatch(CreateMessageBatchOptions
463473
final int maxSize = options.getMaximumSizeInBytes();
464474

465475
return getSendLinkWithRetry("create-batch").flatMap(link -> link.getLinkSize().flatMap(size -> {
466-
final int maximumLinkSize = size > 0 ? size : MAX_MESSAGE_LENGTH_BYTES;
476+
final int maximumLinkSize = Math.min(size > 0 ? size : MAX_MESSAGE_LENGTH_BYTES, MAX_BATCH_SIZE_BYTES);
467477
if (maxSize > maximumLinkSize) {
468478
return monoError(logger,
469479
new IllegalArgumentException(String.format(Locale.US,
470-
"CreateMessageBatchOptions.getMaximumSizeInBytes (%s bytes) is larger than the link size"
471-
+ " (%s bytes).",
480+
"CreateMessageBatchOptions.getMaximumSizeInBytes (%s bytes) is larger than the maximum"
481+
+ " allowed size (%s bytes).",
472482
maxSize, maximumLinkSize)));
473483
}
474-
final int batchSize = maxSize > 0 ? maxSize : maximumLinkSize;
484+
final int batchSize = maxSize > 0 ? Math.min(maxSize, maximumLinkSize) : maximumLinkSize;
475485
return Mono
476486
.just(new ServiceBusMessageBatch(isV2, batchSize, link::getErrorContext, tracer, messageSerializer));
477487
})).onErrorMap(this::mapError);

0 commit comments

Comments
 (0)