Package com.azure.messaging.servicebus
Class ServiceBusSenderClient
java.lang.Object
com.azure.messaging.servicebus.ServiceBusSenderClient
- All Implemented Interfaces:
AutoCloseable
A synchronous sender responsible for sending
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.");
}
}
}
-
Method Summary
Modifier and TypeMethodDescriptionvoidcancelScheduledMessage(long sequenceNumber) Cancels the enqueuing of a scheduled message, if they are not already enqueued.voidcancelScheduledMessages(Iterable<Long> sequenceNumbers) Cancels the enqueuing of scheduled messages, if they are not already enqueued.voidclose()Disposes of theServiceBusSenderClient.voidcommitTransaction(ServiceBusTransactionContext transactionContext) Commits the transaction givenServiceBusTransactionContext.Creates aServiceBusMessageBatchthat can fit as many messages as the transport allows.Creates anServiceBusMessageBatchconfigured with the options specified.Starts a new transaction on Service Bus.Gets the name of the Service Bus resource.Gets the fully qualified namespace.Gets the identifier of the instance ofServiceBusSenderClient.voidrollbackTransaction(ServiceBusTransactionContext transactionContext) Rollbacks the transaction given and all operations associated with it.scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime) Sends a scheduled message to the Azure Service Bus entity this sender is connected to.scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext) Sends a scheduled message to the Azure Service Bus entity this sender is connected to.scheduleMessages(Iterable<ServiceBusMessage> messages, OffsetDateTime scheduledEnqueueTime) Sends a batch of scheduled messages to the Azure Service Bus entity this sender is connected to.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.voidsendMessage(ServiceBusMessage message) Sends a message to a Service Bus queue or topic.voidsendMessage(ServiceBusMessage message, ServiceBusTransactionContext transactionContext) Sends a message to a Service Bus queue or topic.voidSends a message batch to the Azure Service Bus entity this sender is connected to.voidsendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext) Sends a message batch to the Azure Service Bus entity this sender is connected to.voidsendMessages(Iterable<ServiceBusMessage> messages) Sends a set ofServiceBusMessageto a Service Bus queue or topic using a batched approach.voidsendMessages(Iterable<ServiceBusMessage> messages, ServiceBusTransactionContext transactionContext) Sends a set ofServiceBusMessageto a Service Bus queue or topic using a batched approach.
-
Method Details
-
cancelScheduledMessage
public void cancelScheduledMessage(long sequenceNumber) Cancels the enqueuing of a scheduled message, if they are not already enqueued.- Parameters:
sequenceNumber- The sequence number of the message to cancel.- Throws:
IllegalArgumentException- ifsequenceNumberis negative.ServiceBusException- If the message could not be cancelled.IllegalStateException- if sender is already disposed.
-
cancelScheduledMessages
Cancels the enqueuing of scheduled messages, if they are not already enqueued.- Parameters:
sequenceNumbers- The sequence numbers of messages to cancel.- Throws:
NullPointerException- ifsequenceNumbersis null.ServiceBusException- If the messages could not be cancelled.IllegalStateException- if sender is already disposed.
-
createMessageBatch
Creates aServiceBusMessageBatchthat can fit as many messages as the transport allows.- Returns:
- A
ServiceBusMessageBatchthat can fit as many messages as the transport allows. - Throws:
ServiceBusException- if the message batch could not be created.IllegalStateException- if sender is already disposed.
-
createMessageBatch
Creates anServiceBusMessageBatchconfigured with the options specified.- Parameters:
options- A set of options used to configure theServiceBusMessageBatch.- Returns:
- A new
ServiceBusMessageBatchconfigured with the given options. - Throws:
NullPointerException- ifoptionsis null.ServiceBusException- if the message batch could not be created.IllegalStateException- if sender is already disposed.IllegalArgumentException- ifCreateMessageBatchOptions.getMaximumSizeInBytes()is larger than maximum allowed size.
-
getEntityPath
Gets the name of the Service Bus resource.- Returns:
- The name of the Service Bus resource.
-
getFullyQualifiedNamespace
Gets the fully qualified namespace.- Returns:
- The fully qualified namespace.
-
getIdentifier
Gets the identifier of the instance ofServiceBusSenderClient.- Returns:
- The identifier that can identify the instance of
ServiceBusSenderClient.
-
sendMessage
Sends a message to a Service Bus queue or topic.- Parameters:
message- Message to be sent to Service Bus queue or topic.- Throws:
NullPointerException- ifmessageisnull.ServiceBusException- ifmessageis larger than the maximum allowed size of a single message or the message could not be sent.IllegalStateException- if sender is already disposed.
-
sendMessages
Sends a set ofServiceBusMessageto 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.- Parameters:
messages- Messages to be sent to Service Bus queue or topic.- Throws:
NullPointerException- ifmessagesisnull.ServiceBusException- if the message could not be sent ormessageis larger than the maximum size of theServiceBusMessageBatch.IllegalStateException- if sender is already disposed.
-
sendMessages
Sends a message batch to the Azure Service Bus entity this sender is connected to.- Parameters:
batch- of messages which allows client to send maximum allowed size for a batch of messages.- Throws:
NullPointerException- ifbatchisnull.IllegalStateException- if sender is already disposed.ServiceBusException- if the message batch could not be sent.
-
sendMessage
Sends a message to a Service Bus queue or topic.- Parameters:
message- Message to be sent to Service Bus queue or topic.transactionContext- to be set on message before sending to Service Bus.- Throws:
NullPointerException- ifmessage,transactionContextortransactionContext.transactionIdisnull.ServiceBusException- ifmessageis larger than the maximum allowed size of a single message or the message could not be sent.IllegalStateException- if sender is already disposed.
-
sendMessages
public void sendMessages(Iterable<ServiceBusMessage> messages, ServiceBusTransactionContext transactionContext) Sends a set ofServiceBusMessageto 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.- Parameters:
messages- Messages to be sent to Service Bus queue or topic.transactionContext- to be set on message before sending to Service Bus.- Throws:
NullPointerException- ifmessages,transactionContextortransactionContext.transactionIdisnull.ServiceBusException- if the message could not be sent ormessageis larger than the maximum size of theServiceBusMessageBatch.IllegalStateException- if sender is already disposed.
-
sendMessages
public void sendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext) Sends a message batch to the Azure Service Bus entity this sender is connected to.- Parameters:
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.- Throws:
NullPointerException- ifbatch,transactionContextortransactionContext.transactionIdisnull.ServiceBusException- if message batch could not be sent.IllegalStateException- if sender is already disposed.
-
scheduleMessage
Sends a scheduled message to the Azure Service Bus entity this sender is connected to. A scheduled message is enqueued and made available to receivers only at the scheduled enqueue time.- Parameters:
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.- Returns:
- The sequence number of the scheduled message which can be used to cancel the scheduling of the message.
- Throws:
NullPointerException- ifmessageorscheduledEnqueueTimeisnull.ServiceBusException- If the message could not be scheduled.IllegalStateException- if sender is already disposed.
-
scheduleMessage
public Long scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext) Sends a scheduled message to the Azure Service Bus entity this sender is connected to. A scheduled message is enqueued and made available to receivers only at the scheduled enqueue time.- Parameters:
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.- Returns:
- The sequence number of the scheduled message which can be used to cancel the scheduling of the message.
- Throws:
IllegalStateException- if sender is already disposed.NullPointerException- ifmessage,scheduledEnqueueTime,transactionContextortransactionContext.transactionIdisnull.ServiceBusException- If the message could not be scheduled.
-
scheduleMessages
public 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. A scheduled message is enqueued and made available to receivers only at the scheduled enqueue time.- Parameters:
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.- Returns:
- Sequence numbers of the scheduled messages which can be used to cancel the messages.
- Throws:
IllegalStateException- if sender is already disposed.NullPointerException- IfmessagesorscheduledEnqueueTimeisnull.ServiceBusException- If the messages could not be scheduled.
-
scheduleMessages
public 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. A scheduled message is enqueued and made available to receivers only at the scheduled enqueue time.- Parameters:
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.- Returns:
- Sequence numbers of the scheduled messages which can be used to cancel the messages.
- Throws:
IllegalStateException- if sender is already disposed.NullPointerException- Ifmessages,scheduledEnqueueTime,transactionContextortransactionContext.transactionIdisnull.ServiceBusException- If the messages could not be scheduled or themessageis larger than the maximum size of theServiceBusMessageBatch.
-
createTransaction
Starts a new transaction on Service Bus. TheServiceBusTransactionContextshould be passed along to all operations that need to be in this transaction.- Returns:
- A new
ServiceBusTransactionContext. - Throws:
IllegalStateException- if sender is already disposed.ServiceBusException- if a transaction cannot be created.- See Also:
-
commitTransaction
Commits the transaction givenServiceBusTransactionContext.- Parameters:
transactionContext- to be committed.- Throws:
IllegalStateException- if sender is already disposed.NullPointerException- iftransactionContextortransactionContext.transactionIdis null.ServiceBusException- if the transaction could not be committed.- See Also:
-
rollbackTransaction
Rollbacks the transaction given and all operations associated with it.- Parameters:
transactionContext- The transaction to rollback.- Throws:
IllegalStateException- if sender is already disposed.NullPointerException- iftransactionContextortransactionContext.transactionIdis null.ServiceBusException- if the transaction could not be rolled back.- See Also:
-
close
public void close()Disposes of theServiceBusSenderClient. If the client has a dedicated connection, the underlying connection is also closed.- Specified by:
closein interfaceAutoCloseable
-