Package com.azure.messaging.servicebus
Class ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
java.lang.Object
com.azure.messaging.servicebus.ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
- Enclosing class:
- ServiceBusClientBuilder
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:
-
Method Summary
Modifier and TypeMethodDescriptionCreates Service Bus message processor responsible for readingmessages
from a specific queue or subscription.Disables auto-complete and auto-abandon of received messages.maxAutoLockRenewDuration
(Duration maxAutoLockRenewDuration) Sets the amount of time to continue auto-renewing the lock.maxConcurrentCalls
(int maxConcurrentCalls) Max concurrent messages that this processor should process.prefetchCount
(int prefetchCount) Sets the prefetch count of the processor.processError
(Consumer<ServiceBusErrorContext> processError) The error handler for the processor which will be invoked in the event of an error while receiving messages.processMessage
(Consumer<ServiceBusReceivedMessageContext> processMessage) The message processing callback for the processor which will be executed when a message is received.Sets the name of the queue to create a processor for.receiveMode
(ServiceBusReceiveMode receiveMode) Sets the receive mode for the processor.Sets the type of theSubQueue
to connect to.subscriptionName
(String subscriptionName) Sets the name of the subscription in the topic to listen to.Sets the name of the topic.
-
Method Details
-
prefetchCount
Sets the prefetch count of the processor. For bothPEEK_LOCK
andRECEIVE_AND_DELETE
modes the default value is 0. Prefetch speeds up the message flow by aiming to have a message readily available for local retrieval when and before the application starts the processor. Setting a non-zero value will prefetch that number of messages. Setting the value to zero turns prefetch off.- Parameters:
prefetchCount
- The prefetch count.- Returns:
- The modified
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object.
-
queueName
Sets the name of the queue to create a processor for.- Parameters:
queueName
- Name of the queue.- Returns:
- The modified
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object.
-
receiveMode
public ServiceBusClientBuilder.ServiceBusProcessorClientBuilder receiveMode(ServiceBusReceiveMode receiveMode) Sets the receive mode for the processor.- Parameters:
receiveMode
- Mode for receiving messages.- Returns:
- The modified
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object.
-
subQueue
Sets the type of theSubQueue
to connect to. Azure Service Bus queues and subscriptions provide a secondary sub-queue, called a dead-letter queue (DLQ).- Parameters:
subQueue
- The type of the sub queue.- Returns:
- The modified
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object. - See Also:
-
subscriptionName
public ServiceBusClientBuilder.ServiceBusProcessorClientBuilder subscriptionName(String subscriptionName) Sets the name of the subscription in the topic to listen to.topicName(String)
must also be set.- Parameters:
subscriptionName
- Name of the subscription.- Returns:
- The modified
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object. - See Also:
-
topicName
Sets the name of the topic.subscriptionName(String)
must also be set.- Parameters:
topicName
- Name of the topic.- Returns:
- The modified
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object. - See Also:
-
processMessage
public ServiceBusClientBuilder.ServiceBusProcessorClientBuilder processMessage(Consumer<ServiceBusReceivedMessageContext> processMessage) The message processing callback for the processor which will be executed when a message is received.- Parameters:
processMessage
- The message processing consumer that will be executed when a message is received.- Returns:
- The updated
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object.
-
processError
public ServiceBusClientBuilder.ServiceBusProcessorClientBuilder processError(Consumer<ServiceBusErrorContext> processError) The error handler for the processor which will be invoked in the event of an error while receiving messages.- Parameters:
processError
- The error handler which will be executed when an error occurs.- Returns:
- The updated
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object
-
maxAutoLockRenewDuration
public ServiceBusClientBuilder.ServiceBusProcessorClientBuilder maxAutoLockRenewDuration(Duration maxAutoLockRenewDuration) Sets the amount of time to continue auto-renewing the lock. SettingDuration.ZERO
ornull
disables auto-renewal. ForRECEIVE_AND_DELETE
mode, auto-renewal is disabled.- Parameters:
maxAutoLockRenewDuration
- the amount of time to continue auto-renewing the lock.Duration.ZERO
ornull
indicates that auto-renewal is disabled.- Returns:
- The updated
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object. - Throws:
IllegalArgumentException
- If {code maxAutoLockRenewDuration} is negative.
-
maxConcurrentCalls
public ServiceBusClientBuilder.ServiceBusProcessorClientBuilder maxConcurrentCalls(int maxConcurrentCalls) Max concurrent messages that this processor should process. By default, this is set to 1.- Parameters:
maxConcurrentCalls
- max concurrent messages that this processor should process.- Returns:
- The updated
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object. - Throws:
IllegalArgumentException
- if themaxConcurrentCalls
is set to a value less than 1.
-
disableAutoComplete
Disables auto-complete and auto-abandon of received messages. By default, a successfully processed message iscompleted
. If an error happens when the message is processed, it isabandoned
.- Returns:
- The modified
ServiceBusClientBuilder.ServiceBusProcessorClientBuilder
object.
-
buildProcessorClient
Creates Service Bus message processor responsible for readingmessages
from a specific queue or subscription.- Returns:
- An new
ServiceBusProcessorClient
that processes messages from a queue or subscription. - Throws:
IllegalStateException
- ifqueueName
ortopicName
are not set or, both of these fields are set. It is also thrown if the Service BusconnectionString
contains anEntityPath
that does not match one set inqueueName
ortopicName
. Lastly, if atopicName
is set, butsubscriptionName
is not.IllegalArgumentException
- Queue or topic name are not set viaqueueName()
ortopicName()
, respectively.NullPointerException
- if theprocessMessage(Consumer)
orprocessError(Consumer)
callbacks are not set.
-