Source:
public static void main(String[] args) throws InterruptedException { Consumer<ServiceBusReceivedMessageContext> messageProcessor = context -> { ServiceBusReceivedMessage message = context.getMessage(); System.out.println("Received message " + message.getBody().toString()); }; final CountDownLatch countdownLatch = new CountDownLatch(1); Consumer<ServiceBusErrorContext> errorHandler = errorContext -> { if (errorContext.getException() instanceof ServiceBusException) { final ServiceBusException serviceBusException = (ServiceBusException) errorContext.getException(); final ServiceBusFailureReason reason = serviceBusException.getReason(); if (reason == ServiceBusFailureReason.MESSAGING_ENTITY_DISABLED || reason == ServiceBusFailureReason.MESSAGING_ENTITY_NOT_FOUND || reason == ServiceBusFailureReason.UNAUTHORIZED) { System.out.printf("An unrecoverable error occurred. Stopping processing with reason %s: %s\n", reason, serviceBusException.toString()); countdownLatch.countDown(); } else if (reason == ServiceBusFailureReason.MESSAGE_LOCK_LOST) { System.out.printf("Message lock lost for message: %s", errorContext.getException().toString()); } else if (reason == ServiceBusFailureReason.SERVICE_BUSY) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } else { System.out.printf("Error source %s, reason %s, message: %s\n", serviceBusException.getErrorSource(), reason, errorContext.getException().toString()); } } else { System.out.printf("Exception: %s\n", errorContext.getException().toString()); } }; final ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder() .connectionString("<< connection-string >>") .processor() .queueName("<< queue name >>") .processMessage(messageProcessor) .processError(errorHandler) .buildProcessorClient(); System.out.println("Starting the processor"); processorClient.start(); System.out.println("Listening for 10 seconds..."); if (countdownLatch.await(10, TimeUnit.SECONDS)) { System.out.println("Closing processor due to fatal error"); } else { System.out.println("Closing processor"); } processorClient.close(); }

Target:
public static void main(String[] args) throws InterruptedException { Consumer<ServiceBusReceivedMessageContext> messageProcessor = context -> { ServiceBusReceivedMessage message = context.getMessage(); System.out.println("Received message " + message.getBody().toString()); }; final CountDownLatch countdownLatch = new CountDownLatch(1); Consumer<ServiceBusErrorContext> errorHandler = errorContext -> { if (errorContext.getException() instanceof ServiceBusException) { final ServiceBusException serviceBusException = (ServiceBusException) errorContext.getException(); final ServiceBusFailureReason reason = serviceBusException.getReason(); if (reason == ServiceBusFailureReason.MESSAGING_ENTITY_DISABLED || reason == ServiceBusFailureReason.MESSAGING_ENTITY_NOT_FOUND || reason == ServiceBusFailureReason.UNAUTHORIZED) { System.out.printf("An unrecoverable error occurred. Stopping processing with reason %s: %s\n", reason, serviceBusException.getMessage()); countdownLatch.countDown(); } else if (reason == ServiceBusFailureReason.MESSAGE_LOCK_LOST) { System.out.printf("Message lock lost for message: %s", errorContext.getException().toString()); } else if (reason == ServiceBusFailureReason.SERVICE_BUSY) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } else { System.out.printf("Error source %s, reason %s, message: %s\n", serviceBusException.getErrorSource(), reason, errorContext.getException().getMessage()); } } else { System.out.printf("Exception: %s\n", errorContext.getException().toString()); } }; final ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder() .connectionString("<< connection-string >>") .processor() .queueName("<< queue name >>") .processMessage(messageProcessor) .processError(errorHandler) .buildProcessorClient(); System.out.println("Starting the processor"); processorClient.start(); System.out.println("Listening for 10 seconds..."); if (countdownLatch.await(10, TimeUnit.SECONDS)) { System.out.println("Closing processor due to fatal error"); } else { System.out.println("Closing processor"); } processorClient.close(); }