public class ServiceBusSenderClient extends Object implements AutoCloseable
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."); } } }
Modifier and Type | Method and Description |
---|---|
void |
cancelScheduledMessage(long sequenceNumber)
Cancels the enqueuing of an already scheduled message, if it was not already enqueued.
|
void |
close() |
ServiceBusMessageBatch |
createBatch()
Creates a
ServiceBusMessageBatch that can fit as many messages as the transport allows. |
ServiceBusMessageBatch |
createBatch(CreateBatchOptions options)
Creates an
ServiceBusMessageBatch configured with the options specified. |
String |
getEntityPath()
Gets the name of the Service Bus resource.
|
String |
getFullyQualifiedNamespace()
Gets the fully qualified namespace.
|
Long |
scheduleMessage(ServiceBusMessage message,
Instant scheduledEnqueueTime)
Sends a scheduled message to the Azure Service Bus entity this sender is connected to.
|
void |
send(Iterable<ServiceBusMessage> messages)
Sends a set of
ServiceBusMessage to a Service Bus queue or topic using a batched approach. |
void |
send(ServiceBusMessage message)
Sends a message to a Service Bus queue or topic.
|
void |
send(ServiceBusMessageBatch batch)
Sends a message batch to the Azure Service Bus entity this sender is connected to.
|
public String getEntityPath()
public String getFullyQualifiedNamespace()
public void send(ServiceBusMessage message)
message
- Message to be sent to Service Bus queue or topic.NullPointerException
- if message
is null
.public void send(Iterable<ServiceBusMessage> messages)
ServiceBusMessage
to a Service Bus queue or topic using a batched approach.
If the size of messages exceed the maximum size of a single batch, an exception will be triggered and the send
will fail. By default, the message size is the max amount allowed on the link.messages
- Messages to be sent to Service Bus queue or topic.NullPointerException
- if messages
is null
.com.azure.core.amqp.exception.AmqpException
- if messages
is larger than the maximum allowed size of a single batch.public Long scheduleMessage(ServiceBusMessage message, Instant scheduledEnqueueTime)
message
- Message to be sent to the Service Bus Queue or Topic.scheduledEnqueueTime
- Instant at which the message should appear in the Service Bus queue or topic.NullPointerException
- if message
or scheduledEnqueueTime
is null
.public void cancelScheduledMessage(long sequenceNumber)
sequenceNumber
- of the scheduled message to cancel.IllegalArgumentException
- if sequenceNumber
is negative.public ServiceBusMessageBatch createBatch()
ServiceBusMessageBatch
that can fit as many messages as the transport allows.ServiceBusMessageBatch
that can fit as many messages as the transport allows.public ServiceBusMessageBatch createBatch(CreateBatchOptions options)
ServiceBusMessageBatch
configured with the options specified.options
- A set of options used to configure the ServiceBusMessageBatch
.ServiceBusMessageBatch
configured with the given options.NullPointerException
- if options
is null.public void send(ServiceBusMessageBatch batch)
batch
- of messages which allows client to send maximum allowed size for a batch of messages.NullPointerException
- if batch
is null
.public void close()
close
in interface AutoCloseable
Copyright © 2020 Microsoft Corporation. All rights reserved.