Class ServiceBusSessionReceiverClient

java.lang.Object
com.azure.messaging.servicebus.ServiceBusSessionReceiverClient
All Implemented Interfaces:
AutoCloseable

public final class ServiceBusSessionReceiverClient extends Object implements 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();