Class ServiceBusClientBuilder.ServiceBusProcessorClientBuilder

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

public final class ServiceBusClientBuilder.ServiceBusProcessorClientBuilder extends Object
Builder for creating ServiceBusProcessorClient to consume messages from a Service Bus entity. ServiceBusProcessorClients provides a push-based mechanism that notifies the message processing callback when a message is received or the error handle when an error is observed. To create an instance, therefore, configuring the two callbacks - processMessage(Consumer) and processError(Consumer) are necessary. By default, a ServiceBusProcessorClient is configured with auto-completion and auto-lock renewal capabilities.

Sample code to instantiate a processor client

 Consumer<ServiceBusReceivedMessageContext> onMessage = context -> {
     ServiceBusReceivedMessage message = context.getMessage();
     System.out.printf("Processing message. Sequence #: %s. Contents: %s%n",
         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 processor = new ServiceBusClientBuilder()
     .connectionString(connectionString)
     .processor()
     .queueName(queueName)
     .processMessage(onMessage)
     .processError(onError)
     .buildProcessorClient();

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