public final class ServiceBusSenderClient extends Object implements AutoCloseable
ServiceBusMessage
to specific queue or topic on
Azure Service Bus.
Create an instance of sender
// The required parameters is connectionString, a way to authenticate with Service Bus using credentials. // The connectionString/queueName must be set by the application. The 'connectionString' format is shown below. // "Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};SharedAccessKey={key}" ServiceBusSenderClient sender = new ServiceBusClientBuilder() .connectionString(connectionString) .sender() .queueName(queueName) .buildClient();
Send messages to a Service Bus resource
List<ServiceBusMessage> messages = Arrays.asList(new ServiceBusMessage(BinaryData.fromBytes("test-1".getBytes(UTF_8))), new ServiceBusMessage(BinaryData.fromBytes("test-2".getBytes(UTF_8)))); CreateMessageBatchOptions options = new CreateMessageBatchOptions().setMaximumSizeInBytes(10 * 1024); // Creating a batch without options set. ServiceBusMessageBatch batch = sender.createMessageBatch(options); for (ServiceBusMessage message : messages) { if (batch.tryAddMessage(message)) { continue; } sender.sendMessages(batch); }
Send messages using a size-limited ServiceBusMessageBatch
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. CreateMessageBatchOptions options = new CreateMessageBatchOptions() .setMaximumSizeInBytes(256); ServiceBusMessageBatch currentBatch = sender.createMessageBatch(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.tryAddMessage(message)) { sender.sendMessages(currentBatch); currentBatch = sender.createMessageBatch(options); // Add the message we couldn't before. if (!currentBatch.tryAddMessage(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 a scheduled message, if they are not already enqueued.
|
void |
cancelScheduledMessages(Iterable<Long> sequenceNumbers)
Cancels the enqueuing of scheduled messages, if they are not already enqueued.
|
void |
close()
Disposes of the
ServiceBusSenderClient . |
void |
commitTransaction(ServiceBusTransactionContext transactionContext)
Commits the transaction given
ServiceBusTransactionContext . |
ServiceBusMessageBatch |
createMessageBatch()
Creates a
ServiceBusMessageBatch that can fit as many messages as the transport allows. |
ServiceBusMessageBatch |
createMessageBatch(CreateMessageBatchOptions options)
Creates an
ServiceBusMessageBatch configured with the options specified. |
ServiceBusTransactionContext |
createTransaction()
Starts a new transaction on Service Bus.
|
String |
getEntityPath()
Gets the name of the Service Bus resource.
|
String |
getFullyQualifiedNamespace()
Gets the fully qualified namespace.
|
void |
rollbackTransaction(ServiceBusTransactionContext transactionContext)
Rollbacks the transaction given and all operations associated with it.
|
Long |
scheduleMessage(ServiceBusMessage message,
OffsetDateTime scheduledEnqueueTime)
Sends a scheduled message to the Azure Service Bus entity this sender is connected to.
|
Long |
scheduleMessage(ServiceBusMessage message,
OffsetDateTime scheduledEnqueueTime,
ServiceBusTransactionContext transactionContext)
Sends a scheduled message to the Azure Service Bus entity this sender is connected to.
|
Iterable<Long> |
scheduleMessages(Iterable<ServiceBusMessage> messages,
OffsetDateTime scheduledEnqueueTime)
Sends a batch of scheduled messages to the Azure Service Bus entity this sender is connected to.
|
Iterable<Long> |
scheduleMessages(Iterable<ServiceBusMessage> messages,
OffsetDateTime scheduledEnqueueTime,
ServiceBusTransactionContext transactionContext)
Sends a batch of scheduled messages to the Azure Service Bus entity this sender is connected to.
|
void |
sendMessage(ServiceBusMessage message)
Sends a message to a Service Bus queue or topic.
|
void |
sendMessage(ServiceBusMessage message,
ServiceBusTransactionContext transactionContext)
Sends a message to a Service Bus queue or topic.
|
void |
sendMessages(Iterable<ServiceBusMessage> messages)
Sends a set of
ServiceBusMessage to a Service Bus queue or topic using a batched approach. |
void |
sendMessages(Iterable<ServiceBusMessage> messages,
ServiceBusTransactionContext transactionContext)
Sends a set of
ServiceBusMessage to a Service Bus queue or topic using a batched approach. |
void |
sendMessages(ServiceBusMessageBatch batch)
Sends a message batch to the Azure Service Bus entity this sender is connected to.
|
void |
sendMessages(ServiceBusMessageBatch batch,
ServiceBusTransactionContext transactionContext)
Sends a message batch to the Azure Service Bus entity this sender is connected to.
|
public void cancelScheduledMessage(long sequenceNumber)
sequenceNumber
- The sequence number of the message to cancel.IllegalArgumentException
- if sequenceNumber
is negative.ServiceBusException
- If the message could not be cancelled.public void cancelScheduledMessages(Iterable<Long> sequenceNumbers)
sequenceNumbers
- The sequence numbers of messages to cancel.NullPointerException
- if sequenceNumbers
is null.ServiceBusException
- If the messages could not be cancelled.public ServiceBusMessageBatch createMessageBatch()
ServiceBusMessageBatch
that can fit as many messages as the transport allows.ServiceBusMessageBatch
that can fit as many messages as the transport allows.ServiceBusException
- if the message batch could not be created.public ServiceBusMessageBatch createMessageBatch(CreateMessageBatchOptions 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.ServiceBusException
- if the message batch could not be created.public String getEntityPath()
public String getFullyQualifiedNamespace()
public void sendMessage(ServiceBusMessage message)
message
- Message to be sent to Service Bus queue or topic.NullPointerException
- if message
is null
.AmqpException
- if message
is larger than the maximum allowed size of a single message.ServiceBusException
- if the message could not be sent.IllegalStateException
- if sender is already disposed.public void sendMessages(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
.AmqpException
- if messages
are larger than the maximum allowed size of a single batch.ServiceBusException
- if the messages could not be sent.IllegalStateException
- if sender is already disposed.public void sendMessages(ServiceBusMessageBatch batch)
batch
- of messages which allows client to send maximum allowed size for a batch of messages.NullPointerException
- if batch
is null
.IllegalStateException
- if sender is already disposed.ServiceBusException
- if the message batch could not be sent.public void sendMessage(ServiceBusMessage message, ServiceBusTransactionContext transactionContext)
message
- Message to be sent to Service Bus queue or topic.transactionContext
- to be set on message before sending to Service Bus.NullPointerException
- if message
, transactionContext
or
transactionContext.transactionId
is null
.AmqpException
- if message
is larger than the maximum allowed size of a single message.ServiceBusException
- if the message could not be sent.IllegalStateException
- if sender is already disposed.public void sendMessages(Iterable<ServiceBusMessage> messages, ServiceBusTransactionContext transactionContext)
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.transactionContext
- to be set on message before sending to Service Bus.NullPointerException
- if messages
, transactionContext
or
transactionContext.transactionId
is null
.AmqpException
- if messages
are larger than the maximum allowed size of a single batch.ServiceBusException
- if messages could not be sent.IllegalStateException
- if sender is already disposed.public void sendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext)
batch
- of messages which allows client to send maximum allowed size for a batch of messages.transactionContext
- to be set on message before sending to Service Bus.NullPointerException
- if batch
, transactionContext
or
transactionContext.transactionId
is null
.ServiceBusException
- if message batch could not be sent.IllegalStateException
- if sender is already disposed.public Long scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime)
message
- Message to be sent to the Service Bus Queue or Topic.scheduledEnqueueTime
- Datetime at which the message should appear in the Service Bus queue or topic.NullPointerException
- if message
or scheduledEnqueueTime
is null
.ServiceBusException
- If the message could not be scheduled.IllegalStateException
- if sender is already disposed.public Long scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext)
message
- Message to be sent to the Service Bus Queue or Topic.scheduledEnqueueTime
- Datetime at which the message should appear in the Service Bus queue or topic.transactionContext
- to be set on message before sending to Service Bus.IllegalStateException
- if sender is already disposed.NullPointerException
- if message
, scheduledEnqueueTime
, transactionContext
or
transactionContext.transactionId
is null
.ServiceBusException
- If the message could not be scheduled.public Iterable<Long> scheduleMessages(Iterable<ServiceBusMessage> messages, OffsetDateTime scheduledEnqueueTime)
messages
- Messages 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.IllegalStateException
- if sender is already disposed.NullPointerException
- If messages
or scheduledEnqueueTime
is null
.ServiceBusException
- If the messages could not be scheduled.public Iterable<Long> scheduleMessages(Iterable<ServiceBusMessage> messages, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext)
messages
- Messages 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.transactionContext
- Transaction to associate with the operation.IllegalStateException
- if sender is already disposed.NullPointerException
- If messages
, scheduledEnqueueTime
, transactionContext
or
transactionContext.transactionId
is null
.ServiceBusException
- If the messages could not be scheduled.public ServiceBusTransactionContext createTransaction()
ServiceBusTransactionContext
should be passed along to all
operations that need to be in this transaction.ServiceBusTransactionContext
.IllegalStateException
- if sender is already disposed.IllegalStateException
- if the sender is disposed.ServiceBusException
- if a transaction cannot be created.ServiceBusReceiverClient.createTransaction()
public void commitTransaction(ServiceBusTransactionContext transactionContext)
ServiceBusTransactionContext
.transactionContext
- to be committed.IllegalStateException
- if sender is already disposed.NullPointerException
- if transactionContext
or transactionContext.transactionId
is null.ServiceBusException
- if the transaction could not be committed.ServiceBusReceiverClient.commitTransaction(ServiceBusTransactionContext)
public void rollbackTransaction(ServiceBusTransactionContext transactionContext)
transactionContext
- The transaction to rollback.IllegalStateException
- if sender is already disposed.NullPointerException
- if transactionContext
or transactionContext.transactionId
is null.ServiceBusException
- if the transaction could not be rolled back.ServiceBusReceiverClient.rollbackTransaction(ServiceBusTransactionContext)
public void close()
ServiceBusSenderClient
. If the client has a dedicated connection, the underlying
connection is also closed.close
in interface AutoCloseable
Copyright © 2021 Microsoft Corporation. All rights reserved.