ServiceBusMessage to specific queue or topic on Azure Service Bus.Create an instance of sender
// The required parameter is a way to authenticate with Service Bus using credentials. // The connectionString provides a way to authenticate with Service Bus. ServiceBusSenderClient sender = new ServiceBusClientBuilder() .connectionString( "Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};SharedAccessKey={key}") .sender() .queueName("queue-name") .buildClient(); Send messages to a Service Bus resource
List<ServiceBusMessage> messages = Arrays.asList(new ServiceBusMessage("test-1".getBytes(UTF_8)), new ServiceBusMessage("test-2".getBytes(UTF_8))); final CreateBatchOptions options = new CreateBatchOptions().setMaximumSizeInBytes(10 * 1024); // Creating a batch without options set. ServiceBusMessageBatch batch = sender.createBatch(options); for (ServiceBusMessage message : messages) { if (batch.tryAdd(message)) { continue; } sender.send(batch); } Send messages using a size-limited ServiceBusMessageBatch
final List<ServiceBusMessage> telemetryMessages = Arrays.asList(firstMessage, secondMessage, thirdMessage); // Setting `setMaximumSizeInBytes` when creating a batch, limits the size of that batch. // In this case, all the batches created with these options are limited to 256 bytes. final CreateBatchOptions options = new CreateBatchOptions() .setMaximumSizeInBytes(256); ServiceBusMessageBatch currentBatch = sender.createBatch(options); // For each telemetry message, we try to add it to the current batch. // When the batch is full, send it then create another batch to add more mesages to. for (ServiceBusMessage message : telemetryMessages) { if (!currentBatch.tryAdd(message)) { sender.send(currentBatch); currentBatch = sender.createBatch(options); // Add the message we couldn't before. if (!currentBatch.tryAdd(message)) { throw new IllegalArgumentException("Message is too large for an empty batch."); } } } ncG1vNJzZmiZqqq%2Fpr%2FDpJuom6Njr627wWeaqKqVY8SqusOorqxmnprBcHDWnploopGrrnCt2a6pnmWdmsC0rcaipaBlo5q%2Ft7XCnpmuq19se3F6j2aZnqyRY39wr86mZpqypaeycLnErKqan5mjtHC%2FxKutopuVl8K0e7Keqa%2Bhk5qPtr%2BynqWdnaJ4uaqxza1loaydoQ%3D%3D