Example usage for java.util.concurrent CompletableFuture isCancelled

List of usage examples for java.util.concurrent CompletableFuture isCancelled

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture isCancelled.

Prototype

public boolean isCancelled() 

Source Link

Document

Returns true if this CompletableFuture was cancelled before it completed normally.

Usage

From source file:com.microsoft.azure.servicebus.samples.receiveloop.ReceiveLoop.java

CompletableFuture receiveMessagesAsync(IMessageReceiver receiver) {

    CompletableFuture currentTask = new CompletableFuture();

    try {//from w  w  w. ja v  a  2  s .c  o  m
        CompletableFuture.runAsync(() -> {
            while (!currentTask.isCancelled()) {
                try {
                    IMessage message = receiver.receive(Duration.ofSeconds(60));
                    if (message != null) {
                        // receives message is passed to callback
                        if (message.getLabel() != null && message.getContentType() != null
                                && message.getLabel().contentEquals("Scientist")
                                && message.getContentType().contentEquals("application/json")) {

                            byte[] body = message.getBody();
                            Map scientist = GSON.fromJson(new String(body, UTF_8), Map.class);

                            System.out.printf(
                                    "\n\t\t\t\tMessage received: \n\t\t\t\t\t\tMessageId = %s, \n\t\t\t\t\t\tSequenceNumber = %s, \n\t\t\t\t\t\tEnqueuedTimeUtc = %s,"
                                            + "\n\t\t\t\t\t\tExpiresAtUtc = %s, \n\t\t\t\t\t\tContentType = \"%s\",  \n\t\t\t\t\t\tContent: [ firstName = %s, name = %s ]\n",
                                    message.getMessageId(), message.getSequenceNumber(),
                                    message.getEnqueuedTimeUtc(), message.getExpiresAtUtc(),
                                    message.getContentType(),
                                    scientist != null ? scientist.get("firstName") : "",
                                    scientist != null ? scientist.get("name") : "");
                        }
                        receiver.completeAsync(message.getLockToken());
                    }
                } catch (Exception e) {
                    currentTask.completeExceptionally(e);
                }
            }
            currentTask.complete(null);
        });
        return currentTask;
    } catch (Exception e) {
        currentTask.completeExceptionally(e);
    }
    return currentTask;
}

From source file:com.microsoft.azure.servicebus.samples.messagebrowse.MessageBrowse.java

CompletableFuture peekMessagesAsync(IMessageReceiver receiver) {

    CompletableFuture currentTask = new CompletableFuture();
    try {//from  w w  w  .ja v a  2  s .  co m
        CompletableFuture.runAsync(() -> {
            while (!currentTask.isCancelled()) {
                try {
                    IMessage message = receiver.peek();
                    if (message != null) {
                        // receives message is passed to callback
                        if (message.getLabel() != null && message.getContentType() != null
                                && message.getLabel().contentEquals("Scientist")
                                && message.getContentType().contentEquals("application/json")) {

                            byte[] body = message.getBody();
                            Map scientist = GSON.fromJson(new String(body, UTF_8), Map.class);

                            System.out.printf(
                                    "\n\t\t\t\tMessage received: \n\t\t\t\t\t\tMessageId = %s, \n\t\t\t\t\t\tSequenceNumber = %s, \n\t\t\t\t\t\tEnqueuedTimeUtc = %s,"
                                            + "\n\t\t\t\t\t\tExpiresAtUtc = %s, \n\t\t\t\t\t\tContentType = \"%s\",  \n\t\t\t\t\t\tContent: [ firstName = %s, name = %s ]\n",
                                    message.getMessageId(), message.getSequenceNumber(),
                                    message.getEnqueuedTimeUtc(), message.getExpiresAtUtc(),
                                    message.getContentType(),
                                    scientist != null ? scientist.get("firstName") : "",
                                    scientist != null ? scientist.get("name") : "");
                        } else {
                            currentTask.complete(null);
                        }
                    }
                } catch (Exception e) {
                    currentTask.completeExceptionally(e);
                }
            }
            if (!currentTask.isCancelled()) {
                currentTask.complete(null);
            }
        });
        return currentTask;
    } catch (Exception e) {
        currentTask.completeExceptionally(e);
    }
    return currentTask;
}