Class ServiceBusClientBuilder.ServiceBusSessionProcessorClientBuilder

java.lang.Object
com.azure.messaging.servicebus.ServiceBusClientBuilder.ServiceBusSessionProcessorClientBuilder
Enclosing class:
ServiceBusClientBuilder

public final class ServiceBusClientBuilder.ServiceBusSessionProcessorClientBuilder extends Object
Builder for creating ServiceBusProcessorClient to consume messages from a session-based Service Bus entity. ServiceBusProcessorClient processes messages and errors via processMessage(Consumer) and processError(Consumer). When the processor finishes processing a session, it tries to fetch the next session to process.

By default, the processor:

Instantiate a session-enabled processor client

 Consumer<ServiceBusReceivedMessageContext> onMessage = context -> {
     ServiceBusReceivedMessage message = context.getMessage();
     System.out.printf("Processing message. Session: %s, Sequence #: %s. Contents: %s%n",
         message.getSessionId(), message.getSequenceNumber(), message.getBody());
 };

 Consumer<ServiceBusErrorContext> onError = context -> {
     System.out.printf("Error when receiving messages from namespace: '%s'. Entity: '%s'%n",
         context.getFullyQualifiedNamespace(), context.getEntityPath());

     if (context.getException() instanceof ServiceBusException) {
         ServiceBusException exception = (ServiceBusException) context.getException();
         System.out.printf("Error source: %s, reason %s%n", context.getErrorSource(),
             exception.getReason());
     } else {
         System.out.printf("Error occurred: %s%n", context.getException());
     }
 };

 // Retrieve 'connectionString/queueName' from your configuration.

 ServiceBusProcessorClient sessionProcessor = new ServiceBusClientBuilder()
     .connectionString(connectionString)
     .sessionProcessor()
     .queueName(queueName)
     .maxConcurrentSessions(2)
     .processMessage(onMessage)
     .processError(onError)
     .buildProcessorClient();

 // Start the processor in the background
 sessionProcessor.start();
 
See Also: