Example usage for com.google.common.util.concurrent SettableFuture setException

List of usage examples for com.google.common.util.concurrent SettableFuture setException

Introduction

In this page you can find the example usage for com.google.common.util.concurrent SettableFuture setException.

Prototype

@Override
    public boolean setException(Throwable throwable) 

Source Link

Usage

From source file:io.crate.executor.transport.task.elasticsearch.ESCountTask.java

public ESCountTask(ESCountNode node, CrateTransportCountAction transportCountAction) {
    this.transportCountAction = transportCountAction;
    assert node != null;

    final SettableFuture<QueryResult> result = SettableFuture.create();
    results = Arrays.<ListenableFuture<QueryResult>>asList(result);

    String indices[] = node.indices();
    if (node.whereClause().noMatch() || indices.length == 0) {
        result.set(ZERO_RESULT);//from  w  w  w. j  a  v a2  s.c  o m
    } else {
        request = new CountRequest(indices).types(Constants.DEFAULT_MAPPING_TYPE)
                .routing(node.whereClause().clusteredBy().orNull());
        listener = new CountResponseListener(result);
        try {
            request.source(queryBuilder.convert(node.whereClause()), false);
        } catch (IOException e) {
            result.setException(e);
        }
    }
}

From source file:org.opendaylight.distributed.tx.impl.DTXTestTransaction.java

@Override
public CheckedFuture<Void, TransactionCommitFailedException> submit() {
    final SettableFuture<Void> retFuture = SettableFuture.create();

    final Runnable submitResult = new Runnable() {
        @Override//w ww. jav  a 2  s .c  o m
        public void run() {
            try {
                Thread.sleep(delayTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (!submitException) {
                retFuture.set(null);
            } else {
                setSubmitException(false);
                retFuture.setException(new RuntimeException("Submit error"));
            }
            retFuture.notifyAll();
        }
    };

    new Thread(submitResult).start();

    Function<Exception, TransactionCommitFailedException> f = new Function<Exception, TransactionCommitFailedException>() {
        @Nullable
        @Override
        public TransactionCommitFailedException apply(@Nullable Exception e) {
            return new TransactionCommitFailedException("Submit failed", e);
        }
    };

    return Futures.makeChecked(retFuture, f);
}

From source file:com.navercorp.nbasearc.gcp.PhysicalConnection.java

SettableFuture<Boolean> connect() {
    state.set(State.CONNECTING);//from w  w w.j  a va2s .  c o m
    final SettableFuture<Boolean> sf = SettableFuture.create();
    b.connect(ip, port).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            connectionComplete(cf);

            if (cf.cause() != null) {
                sf.setException(cf.cause());
            } else {
                sf.set(true);
            }
        }
    });
    return sf;
}

From source file:com.mirantis.opendaylight.Retrier.java

private void doRetry(SettableFuture<V> promise, int attempt) {
    if (attempt > 0) {
        try {// ww  w .j  a  v a 2s .  c om
            Futures.addCallback(block.get(), new FutureCallback<V>() {
                @Override
                public void onSuccess(V value) {
                    promise.set(value);
                }

                @Override
                public void onFailure(Throwable t) {
                    if (condition.apply(t)) {
                        log.debug("Retrying", t);
                        doRetry(promise, attempt - 1);
                    } else {
                        log.debug(t.getMessage(), t);
                        promise.setException(t);
                    }
                }
            }, executor);
        } catch (RuntimeException e) {
            log.debug("Couldn't get code block to retrying", e);
            promise.setException(e);
        }
    } else {
        promise.setException(new RuntimeException("Number of attempts exeeded"));
    }
}

From source file:com.twitter.heron.statemgr.zookeeper.curator.CuratorStateManager.java

protected ListenableFuture<Boolean> createNode(String path, byte[] data, boolean isEphemeral) {
    final SettableFuture<Boolean> result = SettableFuture.create();

    try {/*from w  w  w .j ava2 s .co  m*/
        client.create().withMode(isEphemeral ? CreateMode.EPHEMERAL : CreateMode.PERSISTENT).forPath(path,
                data);
        LOG.info("Created node for path: " + path);
        result.set(true);

        // Suppress it since forPath() throws Exception
        // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (Exception e) {
        result.setException(new RuntimeException("Could not createNode:", e));
    }
    return result;
}

From source file:com.microsoftopentechnologies.intellij.helpers.azure.sdk.AzureSDKManagerImpl.java

@NotNull
private static ListenableFuture<DeploymentGetResponse> getDeploymentAsync(
        @NotNull final ComputeManagementClient client, @NotNull final String serviceName,
        @NotNull final DeploymentSlot slot) {
    final SettableFuture<DeploymentGetResponse> future = SettableFuture.create();

    ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
        @Override//ww  w .j  ava2 s .  co m
        public void run() {
            try {
                future.set(getDeployment(client, serviceName, slot));
            } catch (Exception e) {
                future.setException(e);
            }
        }
    });

    return future;
}

From source file:com.microsoft.services.sharepoint.ListClient.java

/**
 * Gets user properties./*from   ww  w.  j  av  a  2 s .c o  m*/
 *
 * @return the user properties
 */
public ListenableFuture<String> getUserProperties() {
    final SettableFuture<String> result = SettableFuture.create();

    String url = getSiteUrl() + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";

    ListenableFuture<JSONObject> request = executeRequestJson(url, "GET");

    Futures.addCallback(request, new FutureCallback<JSONObject>() {
        @Override
        public void onFailure(Throwable t) {
            result.setException(t);
        }

        @Override
        public void onSuccess(JSONObject json) {
            result.set(json.toString());
        }
    });
    return result;
}

From source file:com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.LogServiceFilter.java

@Override
public ListenableFuture<ServiceFilterResponse> handleRequest(ServiceFilterRequest request,
        NextServiceFilterCallback nextServiceFilterCallback) {

    final SettableFuture<ServiceFilterResponse> resultFuture = SettableFuture.create();

    String content = request.getContent();
    if (content == null)
        content = "NULL";

    String url = request.getUrl();
    if (url == null)
        url = "";

    Log.d("REQUEST URL", url);
    Log.d("REQUEST CONTENT", content);

    ListenableFuture<ServiceFilterResponse> nextServiceFilterCallbackFuture = nextServiceFilterCallback
            .onNext(request);/*from   ww  w.ja v a  2 s  .  c  o  m*/

    Futures.addCallback(nextServiceFilterCallbackFuture, new FutureCallback<ServiceFilterResponse>() {

        @Override
        public void onFailure(Throwable exception) {
            resultFuture.setException(exception);

        }

        @Override
        public void onSuccess(ServiceFilterResponse response) {
            if (response != null) {
                String content = response.getContent();
                if (content != null) {
                    Log.d("RESPONSE CONTENT", content);
                }
            }

            resultFuture.set(response);
        }
    });

    return resultFuture;
}

From source file:com.microsoft.office365.lists.SharepointListsClient.java

public ListenableFuture<String> getUserProperties() {
    final SettableFuture<String> result = SettableFuture.create();

    String url = getSiteUrl() + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";

    ListenableFuture<JSONObject> request = executeRequestJson(url, "GET");

    Futures.addCallback(request, new FutureCallback<JSONObject>() {
        @Override/*from w w  w.  j a v  a2s  .  c  o  m*/
        public void onFailure(Throwable t) {
            result.setException(t);
        }

        @Override
        public void onSuccess(JSONObject json) {
            result.set(json.toString());
        }
    });
    return result;
}

From source file:com.microsoft.windowsazure.mobileservices.table.sync.MobileServiceJsonSyncTable.java

/**
 * Update an item in the local table and enqueue the operation to be
 * synchronized on context push./*from  w  ww  .  java2  s  . c o  m*/
 *
 * @param item the item to be updated
 * @return A ListenableFuture that is done when the item has been updated.
 */
public ListenableFuture<Void> update(final JsonObject item) {
    final MobileServiceJsonSyncTable thisTable = this;
    final SettableFuture<Void> result = SettableFuture.create();

    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                thisTable.updateContext(item);

                result.set(null);
            } catch (Throwable throwable) {
                result.setException(throwable);
            }
        }
    }).start();

    return result;
}