Package com.azure.messaging.servicebus
Class ServiceBusSessionReceiverClient
java.lang.Object
com.azure.messaging.servicebus.ServiceBusSessionReceiverClient
- All Implemented Interfaces:
AutoCloseable
This synchronous session receiver client is used to acquire session locks from a queue or topic and create
ServiceBusReceiverClient
instances that are tied to the locked sessions.
Receive messages from a specific session
Use acceptSession(String)
to acquire the lock of a session if you know the session id.
// The connectionString/sessionQueueName must be set by the application. The 'connectionString' format is shown below. // "Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};SharedAccessKey={key}" ServiceBusSessionReceiverClient sessionReceiver = new ServiceBusClientBuilder() .connectionString(connectionString) .sessionReceiver() .queueName(sessionQueueName) .buildClient(); ServiceBusReceiverClient receiver = sessionReceiver.acceptSession("<< my-session-id >>"); // Use the receiver and finally close it along with the sessionReceiver. receiver.close(); sessionReceiver.close();
Receive messages from the first available session
Use acceptNextSession()
to acquire the lock of the next available session without specifying the session
id.
// The connectionString/sessionQueueName must be set by the application. The 'connectionString' format is shown below. // "Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};SharedAccessKey={key}" ServiceBusSessionReceiverClient sessionReceiver = new ServiceBusClientBuilder() .connectionString( "Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};SharedAccessKey={key}") .sessionReceiver() .queueName("<< QUEUE NAME >>") .buildClient(); ServiceBusReceiverClient receiver = sessionReceiver.acceptNextSession(); // Use the receiver and finally close it along with the sessionReceiver. receiver.close(); sessionReceiver.close();
-
Method Summary
Modifier and TypeMethodDescriptionAcquires a session lock for the next available session and creates aServiceBusReceiverClient
to receive messages from the session.acceptSession
(String sessionId) Acquires a session lock forsessionId
and create aServiceBusReceiverClient
to receive messages from the session.void
close()
-
Method Details
-
acceptNextSession
Acquires a session lock for the next available session and creates aServiceBusReceiverClient
to receive messages from the session. It will wait until a session is available if no one is available immediately.- Returns:
- A
ServiceBusReceiverClient
that is tied to the available session. - Throws:
UnsupportedOperationException
- if the queue or topic subscription is not session-enabled.com.azure.core.amqp.exception.AmqpException
- if the operation times out. The timeout duration is the tryTimeout of when you build this client with theServiceBusClientBuilder.retryOptions(AmqpRetryOptions)
.
-
acceptSession
Acquires a session lock forsessionId
and create aServiceBusReceiverClient
to receive messages from the session. If the session is already locked by another client, anAmqpException
is thrown immediately.- Parameters:
sessionId
- The session id.- Returns:
- A
ServiceBusReceiverClient
that is tied to the specified session. - Throws:
NullPointerException
- ifsessionId
is null.IllegalArgumentException
- ifsessionId
is empty.UnsupportedOperationException
- if the queue or topic subscription is not session-enabled.ServiceBusException
- if the lock cannot be acquired.com.azure.core.amqp.exception.AmqpException
- if the operation times out. The timeout duration is the tryTimeout of when you build this client with theServiceBusClientBuilder.retryOptions(AmqpRetryOptions)
.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-