Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[service bus] Update ServiceBusProcessor sample to demo how to write a long-running processor. #17633

Merged
4 commits merged into from
Nov 18, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.messaging.servicebus;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

Expand All @@ -24,18 +25,40 @@ public static void main(String[] args) throws InterruptedException {
System.out.println("Received message " + message.getBody().toString());
};

final CountDownLatch countdownLatch = new CountDownLatch(1);

// Consumer that handles any errors that occur when receiving messages
Consumer<ServiceBusErrorContext> errorHandler = errorContext -> {
System.out.println("Error when receiving messages " + errorContext.getException().getMessage());
if (errorContext.getException() instanceof ServiceBusException) {
ServiceBusException serviceBusException = (ServiceBusException) errorContext.getException();
System.out.printf("Error source %s, reason %s\n", serviceBusException.getErrorSource(),
serviceBusException.getReason());
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 {
// choosing an arbitrary amount of time to wait.
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());
}
};

// Create an instance of the processor through the ServiceBusClientBuilder
ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
final ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
.connectionString("<< connection-string >>")
.processor()
.queueName("<< queue name >>")
Expand All @@ -46,16 +69,13 @@ public static void main(String[] args) throws InterruptedException {
System.out.println("Starting the processor");
processorClient.start();

TimeUnit.SECONDS.sleep(10);
System.out.println("Stopping the processor");
processorClient.stop();

TimeUnit.SECONDS.sleep(10);
System.out.println("Resuming 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");
}

TimeUnit.SECONDS.sleep(10);
System.out.println("Closing the processor");
processorClient.close();
}
}