Package com.azure.messaging.servicebus
Class ServiceBusReceiverClient
java.lang.Object
com.azure.messaging.servicebus.ServiceBusReceiverClient
- All Implemented Interfaces:
AutoCloseable
A synchronous receiver responsible for receiving
ServiceBusReceivedMessage
from a specific queue or
topic on Azure Service Bus.
Create an instance of receiver
// 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}" ServiceBusReceiverClient receiver = new ServiceBusClientBuilder() .connectionString(connectionString) .receiver() .queueName(queueName) .buildClient(); // Use the receiver and finally close it. receiver.close();
-
Method Summary
Modifier and TypeMethodDescriptionvoid
abandon
(ServiceBusReceivedMessage message) Abandons amessage
.void
abandon
(ServiceBusReceivedMessage message, AbandonOptions options) Abandons amessage
and updates the message's properties.void
close()
Disposes of the consumer by closing the underlying links to the service.void
commitTransaction
(ServiceBusTransactionContext transactionContext) Commits the transaction and all the operations associated with it.void
complete
(ServiceBusReceivedMessage message) Completes amessage
.void
complete
(ServiceBusReceivedMessage message, CompleteOptions options) Completes amessage
.Starts a new transaction on Service Bus.void
deadLetter
(ServiceBusReceivedMessage message) Moves amessage
to the dead-letter sub-queue.void
deadLetter
(ServiceBusReceivedMessage message, DeadLetterOptions options) Moves amessage
to the dead-letter sub-queue with dead-letter reason, error description, and/or modified properties.void
defer
(ServiceBusReceivedMessage message) Defers amessage
.void
defer
(ServiceBusReceivedMessage message, DeferOptions options) Defers amessage
using its lock token with modified message property.Gets the Service Bus resource this client interacts with.Gets the fully qualified Service Bus namespace that the connection is associated with.Gets the identifier of the instance ofServiceBusReceiverClient
.Gets the SessionId of the session if this receiver is a session receiver.byte[]
Gets the state of the session if this receiver is a session receiver.Reads the next active message without changing the state of the receiver or the message source.peekMessage
(long sequenceNumber) Starting from the given sequence number, reads next the active message without changing the state of the receiver or the message source.com.azure.core.util.IterableStream<ServiceBusReceivedMessage>
peekMessages
(int maxMessages) Reads the next batch of active messages without changing the state of the receiver or the message source.com.azure.core.util.IterableStream<ServiceBusReceivedMessage>
peekMessages
(int maxMessages, long sequenceNumber) Starting from the given sequence number, reads the next batch of active messages without changing the state of the receiver or the message source.receiveDeferredMessage
(long sequenceNumber) Receives a deferredmessage
.com.azure.core.util.IterableStream<ServiceBusReceivedMessage>
receiveDeferredMessageBatch
(Iterable<Long> sequenceNumbers) Receives a batch of deferredmessages
.com.azure.core.util.IterableStream<ServiceBusReceivedMessage>
receiveMessages
(int maxMessages) Receives an iterable stream ofmessages
from the Service Bus entity.com.azure.core.util.IterableStream<ServiceBusReceivedMessage>
receiveMessages
(int maxMessages, Duration maxWaitTime) Receives an iterable stream ofmessages
from the Service Bus entity.Renews the lock on the specified message.void
renewMessageLock
(ServiceBusReceivedMessage message, Duration maxLockRenewalDuration, Consumer<Throwable> onError) Starts the auto lock renewal for a message with the given lock.Sets the state of the session if this receiver is a session receiver.void
renewSessionLock
(Duration maxLockRenewalDuration, Consumer<Throwable> onError) Starts the auto lock renewal for the session that this receiver works for.void
rollbackTransaction
(ServiceBusTransactionContext transactionContext) Rollbacks the transaction given and all operations associated with it.void
setSessionState
(byte[] sessionState) Sets the state of the session if this receiver is a session receiver.
-
Method Details
-
getFullyQualifiedNamespace
Gets the fully qualified Service Bus namespace that the connection is associated with. This is likely similar to{yournamespace}.servicebus.windows.net
.- Returns:
- The fully qualified Service Bus namespace that the connection is associated with.
-
getEntityPath
Gets the Service Bus resource this client interacts with.- Returns:
- The Service Bus resource this client interacts with.
-
getSessionId
Gets the SessionId of the session if this receiver is a session receiver.- Returns:
- The SessionId or null if this is not a session receiver.
-
getIdentifier
Gets the identifier of the instance ofServiceBusReceiverClient
.- Returns:
- The identifier that can identify the instance of
ServiceBusReceiverClient
.
-
abandon
Abandons amessage
. This will make the message available again for processing. Abandoning a message will increase the delivery count on the message.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.- Throws:
NullPointerException
- ifmessage
is null.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if receiver is already disposed.ServiceBusException
- if the message could not be abandoned.IllegalArgumentException
- if the message has either been deleted or already settled.
-
abandon
Abandons amessage
and updates the message's properties. This will make the message available again for processing. Abandoning a message will increase the delivery count on the message.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.options
- The options to set while abandoning the message.- Throws:
NullPointerException
- ifmessage
oroptions
is null. Also iftransactionContext.transactionId
is null whenoptions.transactionContext
is specified.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if receiver is already disposed.IllegalArgumentException
- if the message has either been deleted or already settled.ServiceBusException
- if the message could not be abandoned.
-
complete
Completes amessage
. This will delete the message from the service.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.- Throws:
NullPointerException
- ifmessage
is null.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if receiver is already disposed.IllegalArgumentException
- if the message has either been deleted or already settled.ServiceBusException
- if the message could not be completed.
-
complete
Completes amessage
. This will delete the message from the service.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.options
- Options used to complete the message.- Throws:
NullPointerException
- ifmessage
oroptions
is null. Also iftransactionContext.transactionId
is null whenoptions.transactionContext
is specified.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if receiver is already disposed.IllegalArgumentException
- if the message has either been deleted or already settled.ServiceBusException
- if the message could not be completed.
-
defer
Defers amessage
. This will move message into the deferred subqueue.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.- Throws:
NullPointerException
- ifmessage
is null.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if receiver is already disposed.ServiceBusException
- if the message could not be deferred.IllegalArgumentException
- if the message has either been deleted or already settled.- See Also:
-
defer
Defers amessage
using its lock token with modified message property. This will move message into the deferred sub-queue.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.options
- Options used to defer the message.- Throws:
NullPointerException
- ifmessage
oroptions
is null. Also iftransactionContext.transactionId
is null whenoptions.transactionContext
is specified.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if receiver is already disposed.ServiceBusException
- if the message could not be deferred.IllegalArgumentException
- if the message has either been deleted or already settled.- See Also:
-
deadLetter
Moves amessage
to the dead-letter sub-queue.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.- Throws:
NullPointerException
- ifmessage
is null.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if receiver is already disposed.ServiceBusException
- if the message could not be dead-lettered.IllegalArgumentException
- if the message has either been deleted or already settled.- See Also:
-
deadLetter
Moves amessage
to the dead-letter sub-queue with dead-letter reason, error description, and/or modified properties.- Parameters:
message
- TheServiceBusReceivedMessage
to perform this operation.options
- Options used to dead-letter the message.- Throws:
NullPointerException
- ifmessage
oroptions
is null. Also iftransactionContext.transactionId
is null whenoptions.transactionContext
is specified.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received frompeekMessage
.IllegalStateException
- if the receiver is already disposed of.ServiceBusException
- if the message could not be dead-lettered.IllegalArgumentException
- if the message has either been deleted or already settled.- See Also:
-
getSessionState
public byte[] getSessionState()Gets the state of the session if this receiver is a session receiver.- Returns:
- The session state or null if there is no state set for the session.
- Throws:
IllegalStateException
- if the receiver is a non-session receiver or receiver is already disposed.ServiceBusException
- if the session state could not be acquired.
-
peekMessage
Reads the next active message without changing the state of the receiver or the message source. The first call topeekMessage()
fetches the first active message for this receiver. Each subsequent call fetches the subsequent message in the entity.- Returns:
- A peeked
ServiceBusReceivedMessage
. - Throws:
IllegalStateException
- if the receiver is already disposed.ServiceBusException
- if an error occurs while peeking at the message.- See Also:
-
peekMessage
Starting from the given sequence number, reads next the active message without changing the state of the receiver or the message source.- Parameters:
sequenceNumber
- The sequence number from where to read the message.- Returns:
- A peeked
ServiceBusReceivedMessage
. - Throws:
IllegalStateException
- if the receiver is already disposed.ServiceBusException
- if an error occurs while peeking at the message.- See Also:
-
peekMessages
Reads the next batch of active messages without changing the state of the receiver or the message source.- Parameters:
maxMessages
- The maximum number of messages to peek.- Returns:
- An
IterableStream
ofmessages
that are peeked. - Throws:
IllegalArgumentException
- ifmaxMessages
is not a positive integer.IllegalStateException
- if the receiver is already disposed.ServiceBusException
- if an error occurs while peeking at messages.- See Also:
-
peekMessages
public com.azure.core.util.IterableStream<ServiceBusReceivedMessage> peekMessages(int maxMessages, long sequenceNumber) Starting from the given sequence number, reads the next batch of active messages without changing the state of the receiver or the message source.- Parameters:
maxMessages
- The number of messages.sequenceNumber
- The sequence number from where to start reading messages.- Returns:
- An
IterableStream
ofmessages
peeked. - Throws:
IllegalArgumentException
- ifmaxMessages
is not a positive integer.IllegalStateException
- if the receiver is already disposed.ServiceBusException
- if an error occurs while peeking at messages.- See Also:
-
receiveMessages
public com.azure.core.util.IterableStream<ServiceBusReceivedMessage> receiveMessages(int maxMessages) Receives an iterable stream ofmessages
from the Service Bus entity. The receive operation will wait for a default 1 minute for receiving a message before it times out. You can override it by usingreceiveMessages(int, Duration)
.- Parameters:
maxMessages
- The maximum number of messages to receive.- Returns:
- An
IterableStream
of at mostmaxMessages
messages from the Service Bus entity. - Throws:
IllegalArgumentException
- ifmaxMessages
is zero or a negative value.IllegalStateException
- if the receiver is already disposed.ServiceBusException
- if an error occurs while receiving messages.- See Also:
-
receiveMessages
public com.azure.core.util.IterableStream<ServiceBusReceivedMessage> receiveMessages(int maxMessages, Duration maxWaitTime) Receives an iterable stream ofmessages
from the Service Bus entity. The default receive mode isServiceBusReceiveMode.PEEK_LOCK
unless it is changed during creation ofServiceBusReceiverClient
usingServiceBusClientBuilder.ServiceBusReceiverClientBuilder.receiveMode(ServiceBusReceiveMode)
.- Parameters:
maxMessages
- The maximum number of messages to receive.maxWaitTime
- The time the client waits for receiving a message before it times out.- Returns:
- An
IterableStream
of at mostmaxMessages
messages from the Service Bus entity. - Throws:
IllegalArgumentException
- ifmaxMessages
ormaxWaitTime
is zero or a negative value.IllegalStateException
- if the receiver is already disposed.NullPointerException
- ifmaxWaitTime
is null.ServiceBusException
- if an error occurs while receiving messages.- See Also:
-
receiveDeferredMessage
Receives a deferredmessage
. Deferred messages can only be received by using sequence number.- Parameters:
sequenceNumber
- Thesequence number
of the message.- Returns:
- A deferred message with the matching
sequenceNumber
. - Throws:
IllegalStateException
- if receiver is already disposed.ServiceBusException
- if deferred message cannot be received.
-
receiveDeferredMessageBatch
public com.azure.core.util.IterableStream<ServiceBusReceivedMessage> receiveDeferredMessageBatch(Iterable<Long> sequenceNumbers) Receives a batch of deferredmessages
. Deferred messages can only be received by using sequence number.- Parameters:
sequenceNumbers
- The sequence numbers of the deferred messages.- Returns:
- An
IterableStream
of deferredmessages
. - Throws:
NullPointerException
- ifsequenceNumbers
is null.IllegalStateException
- if receiver is already disposed.ServiceBusException
- if deferred messages cannot be received.
-
renewMessageLock
Renews the lock on the specified message. The lock will be renewed based on the setting specified on the entity. When a message is received inServiceBusReceiveMode.PEEK_LOCK
mode, the message is locked on the server for this receiver instance for a duration as specified during the Queue creation (LockDuration). If processing of the message requires longer than this duration, the lock needs to be renewed. For each renewal, the lock is reset to the entity's LockDuration value.- Parameters:
message
- TheServiceBusReceivedMessage
to perform lock renewal.- Returns:
- The new expiration time for the message.
- Throws:
NullPointerException
- ifmessage
ormessage.getLockToken()
is null.UnsupportedOperationException
- if the receiver was opened inServiceBusReceiveMode.RECEIVE_AND_DELETE
mode or if the message was received from peekMessage.IllegalStateException
- if the receiver is a session receiver or receiver is already disposed.IllegalArgumentException
- ifmessage.getLockToken()
is an empty value.
-
renewMessageLock
public void renewMessageLock(ServiceBusReceivedMessage message, Duration maxLockRenewalDuration, Consumer<Throwable> onError) Starts the auto lock renewal for a message with the given lock.- Parameters:
message
- TheServiceBusReceivedMessage
to perform auto-lock renewal.maxLockRenewalDuration
- Maximum duration to keep renewing the lock token.onError
- A function to call when an error occurs during lock renewal.- Throws:
NullPointerException
- ifmessage
,message.getLockToken()
, ormaxLockRenewalDuration
is null.IllegalStateException
- if the receiver is a session receiver or the receiver is disposed.IllegalArgumentException
- ifmessage.getLockToken()
is an empty value.ServiceBusException
- If the message cannot be renewed.
-
renewSessionLock
Sets the state of the session if this receiver is a session receiver.- Returns:
- The next expiration time for the session lock.
- Throws:
IllegalStateException
- if the receiver is a non-session receiver or receiver is already disposed.ServiceBusException
- if the session lock cannot be renewed.
-
renewSessionLock
Starts the auto lock renewal for the session that this receiver works for.- Parameters:
maxLockRenewalDuration
- Maximum duration to keep renewing the session.onError
- A function to call when an error occurs during lock renewal.- Throws:
NullPointerException
- ifsessionId
ormaxLockRenewalDuration
is null.IllegalArgumentException
- ifsessionId
is an empty string ormaxLockRenewalDuration
is negative.IllegalStateException
- if the receiver is a non-session receiver or the receiver is disposed.ServiceBusException
- if the session lock renewal operation cannot be started.
-
setSessionState
public void setSessionState(byte[] sessionState) Sets the state of the session if this receiver is a session receiver.- Parameters:
sessionState
- State to set on the session.- Throws:
IllegalStateException
- if the receiver is a non-session receiver or receiver is already disposed.ServiceBusException
- if the session state cannot be set.
-
createTransaction
Starts a new transaction on Service Bus. TheServiceBusTransactionContext
should be passed along to all operations that need to be in this transaction.Creating and using a transaction
ServiceBusTransactionContext transaction = receiver.createTransaction(); // Process messages and associate operations with the transaction. ServiceBusReceivedMessage deferredMessage = receiver.receiveDeferredMessage(sequenceNumber); receiver.complete(deferredMessage, new CompleteOptions().setTransactionContext(transaction)); receiver.abandon(receivedMessage, new AbandonOptions().setTransactionContext(transaction)); receiver.commitTransaction(transaction);
- Returns:
- A new
ServiceBusTransactionContext
. - Throws:
IllegalStateException
- if the receiver is already disposed.ServiceBusException
- if a transaction cannot be created.
-
commitTransaction
Commits the transaction and all the operations associated with it.Creating and using a transaction
ServiceBusTransactionContext transaction = receiver.createTransaction(); // Process messages and associate operations with the transaction. ServiceBusReceivedMessage deferredMessage = receiver.receiveDeferredMessage(sequenceNumber); receiver.complete(deferredMessage, new CompleteOptions().setTransactionContext(transaction)); receiver.abandon(receivedMessage, new AbandonOptions().setTransactionContext(transaction)); receiver.commitTransaction(transaction);
- Parameters:
transactionContext
- The transaction to be commit.- Throws:
IllegalStateException
- if the receiver is already disposed.NullPointerException
- iftransactionContext
ortransactionContext.transactionId
is null.ServiceBusException
- if the transaction could not be committed.
-
rollbackTransaction
Rollbacks the transaction given and all operations associated with it.Creating and using a transaction
ServiceBusTransactionContext transaction = receiver.createTransaction(); // Process messages and associate operations with the transaction. ServiceBusReceivedMessage deferredMessage = receiver.receiveDeferredMessage(sequenceNumber); receiver.complete(deferredMessage, new CompleteOptions().setTransactionContext(transaction)); receiver.abandon(receivedMessage, new AbandonOptions().setTransactionContext(transaction)); receiver.commitTransaction(transaction);
- Parameters:
transactionContext
- The transaction to be rollback.- Throws:
IllegalStateException
- if the receiver is alread disposed.NullPointerException
- iftransactionContext
ortransactionContext.transactionId
is null.ServiceBusException
- if the transaction could not be rolled back.
-
close
public void close()Disposes of the consumer by closing the underlying links to the service.- Specified by:
close
in interfaceAutoCloseable
-