Skip to content

Instantly share code, notes, and snippets.

@zarinfam
Created July 29, 2019 10:03
Show Gist options
  • Save zarinfam/fd0c64f747bfdcf28bbd51baf736dc55 to your computer and use it in GitHub Desktop.
Save zarinfam/fd0c64f747bfdcf28bbd51baf736dc55 to your computer and use it in GitHub Desktop.
Created with Copy to Gist
public synchronized void printText(String text) throws PrintingException {
final CompletableFuture<SuccessResult> result = new CompletableFuture();
PrintingListener printingListener = new PrintingListener() {
@Override
public void onSuccess(SuccessResult successResult) {
result.complete(successResult);
}
@Override
public void onFailure(FailureResult failureResult) {
result.completeExceptionally(new PrintingException(PrintTask.RESULT_FAILURE));
}
@Override
public void onTimeout(TimeoutResult timeoutResult) {
result.completeExceptionally(new PrintingException(PrintTask.RESULT_TIMEOUT));
}
};
printerApi.startPrinting(text, printingListener);
try {
result.join();
} catch (CompletionException ex) {
throw (PrintingException) ex.getCause();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment