Example usage for com.google.common.util.concurrent FutureCallback FutureCallback

List of usage examples for com.google.common.util.concurrent FutureCallback FutureCallback

Introduction

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

Prototype

FutureCallback

Source Link

Usage

From source file:io.bitsquare.trade.protocol.trade.offerer.tasks.SignAndPublishDepositTx.java

public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler,
        WalletService walletService, String preparedOffererDepositTxAsHex, String signedTakerDepositTxAsHex,
        String txConnOutAsHex, String txScriptSigAsHex, long offererTxOutIndex, long takerTxOutIndex) {
    log.trace("Run task");
    try {//from  w w w.j  a v a 2s. c  om
        walletService.offererSignAndPublishTx(preparedOffererDepositTxAsHex, signedTakerDepositTxAsHex,
                txConnOutAsHex, txScriptSigAsHex, offererTxOutIndex, takerTxOutIndex,
                new FutureCallback<Transaction>() {
                    @Override
                    public void onSuccess(Transaction transaction) {
                        log.trace("offererSignAndPublishTx succeeded " + transaction);
                        resultHandler.onResult(transaction);
                    }

                    @Override
                    public void onFailure(@NotNull Throwable t) {
                        log.error("offererSignAndPublishTx faultHandler.onFault:" + t);
                        exceptionHandler.handleException(t);
                    }
                });
    } catch (Exception e) {
        log.error("offererSignAndPublishTx faultHandler.onFault:" + e);
        exceptionHandler.handleException(e);
    }
}

From source file:org.opendaylight.netvirt.elan.utils.ElanClusterUtils.java

public static void runOnlyInLeaderNode(EntityOwnershipService entityOwnershipService, final Runnable job,
        final String jobDescription) {
    ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
            entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
            HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
    Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
        @Override//from   w  ww.  ja v  a 2  s.  c o m
        public void onSuccess(Boolean isOwner) {
            if (isOwner) {
                job.run();
            } else {
                LOG.trace("job is not run as i m not cluster owner desc :{} ", jobDescription);
            }
        }

        @Override
        public void onFailure(Throwable error) {
            LOG.error("Failed to identity cluster owner ", error);
        }
    });
}

From source file:io.druid.java.util.common.concurrent.ListenableFutures.java

/**
 * Guava 19 changes the Futures.transform signature so that the async form is different. This is here as a
 * compatability layer until such a time as druid only supports Guava 19 or later, in which case
 * Futures.transformAsync should be used
 *
 * This is NOT copied from guava./*w w w . ja  v  a2 s  .  co  m*/
 */
public static <I, O> ListenableFuture<O> transformAsync(final ListenableFuture<I> inFuture,
        final Function<I, ListenableFuture<O>> transform) {
    final SettableFuture<O> finalFuture = SettableFuture.create();
    Futures.addCallback(inFuture, new FutureCallback<I>() {
        @Override
        public void onSuccess(@Nullable I result) {
            final ListenableFuture<O> transformFuture = transform.apply(result);
            Futures.addCallback(transformFuture, new FutureCallback<O>() {
                @Override
                public void onSuccess(@Nullable O result) {
                    finalFuture.set(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    finalFuture.setException(t);
                }
            });
        }

        @Override
        public void onFailure(Throwable t) {
            finalFuture.setException(t);
        }
    });
    return finalFuture;
}

From source file:com.microsoft.sharepointservices.http.SharepointCookieCredentials.java

/**
 * Request credentials./* w w w . j  a v a2 s  . c  om*/
 *
 * @param sharepointSiteUrl the sharepoint site url
 * @param activity the activity
 * @return the listenable future
 */
public static ListenableFuture<CookieCredentials> requestCredentials(String sharepointSiteUrl,
        Activity activity) {
    final SettableFuture<CookieCredentials> future = SettableFuture.create();

    ListenableFuture<String> login = showLoginForCookies(activity, sharepointSiteUrl);

    Futures.addCallback(login, new FutureCallback<String>() {
        @Override
        public void onFailure(Throwable t) {
            future.setException(t);
        }

        @Override
        public void onSuccess(String cookies) {
            future.set(new CookieCredentials(cookies));
        }
    });

    return future;
}

From source file:com.torodb.common.util.ListeningFutureToCompletableFuture.java

public static <T> CompletableFuture<T> toCompletableFuture(final ListenableFuture<T> listenableFuture) {
    //create an instance of CompletableFuture
    CompletableFuture<T> completable = new CompletableFuture<T>() {
        @Override/*  w w  w .j  a  v a2s .  co  m*/
        public boolean cancel(boolean mayInterruptIfRunning) {
            // propagate cancel to the listenable future
            boolean result = listenableFuture.cancel(mayInterruptIfRunning);
            super.cancel(mayInterruptIfRunning);
            return result;
        }
    };

    // add callback
    Futures.addCallback(listenableFuture, new FutureCallback<T>() {
        @Override
        @SuppressFBWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
        public void onSuccess(T result) {
            completable.complete(result);
        }

        @Override
        public void onFailure(Throwable t) {
            completable.completeExceptionally(t);
        }
    });
    return completable;
}

From source file:com.spotify.apollo.route.Middleware.java

static <T> AsyncHandler<T> guavaToAsync(ListenableFutureHandler<T> listenableFutureHandler) {
    return requestContext -> {
        CompletableFuture<T> future = new CompletableFuture<>();

        addCallback(listenableFutureHandler.invoke(requestContext), new FutureCallback<T>() {
            @Override/*  w w  w.ja v a  2 s .c om*/
            public void onSuccess(T result) {
                future.complete(result);
            }

            @Override
            public void onFailure(Throwable t) {
                future.completeExceptionally(t);
            }
        });

        return future;
    };
}

From source file:io.bitsquare.trade.protocol.trade.taker.tasks.SignAndPublishPayoutTx.java

public static void run(ResultHandler resultHandler, ExceptionHandler exceptionHandler,
        WalletService walletService, String tradeId, String depositTxAsHex, String offererSignatureR,
        String offererSignatureS, Coin offererPaybackAmount, Coin takerPaybackAmount,
        String offererPayoutAddress) {
    log.trace("Run task");
    try {//from  w ww. java 2s.co  m

        walletService.takerSignsAndSendsTx(depositTxAsHex, offererSignatureR, offererSignatureS,
                offererPaybackAmount, takerPaybackAmount, offererPayoutAddress, tradeId,
                new FutureCallback<Transaction>() {
                    @Override
                    public void onSuccess(Transaction transaction) {
                        log.debug("takerSignsAndSendsTx " + transaction);
                        String payoutTxAsHex = Utils.HEX.encode(transaction.bitcoinSerialize());
                        resultHandler.onResult(transaction, payoutTxAsHex);
                    }

                    @Override
                    public void onFailure(@NotNull Throwable t) {
                        log.error("Exception at takerSignsAndSendsTx " + t);
                        exceptionHandler.handleException(t);
                    }
                });
    } catch (Exception e) {
        log.error("Exception at takerSignsAndSendsTx " + e);
        exceptionHandler.handleException(e);
    }
}

From source file:org.opendaylight.restconf.restful.utils.FutureCallbackTx.java

/**
 * Add callback to the future object/*from  w  w  w  .jav  a  2 s  .  c  o  m*/
 *
 * @param listenableFuture
 *            - future object
 * @param txType
 *            - type of operation (READ, POST, PUT, DELETE)
 * @param dataFactory
 *            - factory setting result
 */
static <T, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture,
        final String txType, final FutureDataFactory<T> dataFactory) {
    final CountDownLatch responseWaiter = new CountDownLatch(1);
    Futures.addCallback(listenableFuture, new FutureCallback<T>() {

        @Override
        public void onFailure(final Throwable t) {
            responseWaiter.countDown();
            handlingLoggerAndValues(t, txType, null, dataFactory);
        }

        @Override
        public void onSuccess(final T result) {
            handlingLoggerAndValues(null, txType, result, dataFactory);
            responseWaiter.countDown();
        }

    });
    try {
        responseWaiter.await();
    } catch (final Exception e) {
        final String msg = "Problem while waiting for response";
        LOG.warn(msg);
        throw new RestconfDocumentedException(msg, e);
    }
}

From source file:org.opendaylight.vpnservice.elan.utils.ElanClusterUtils.java

public static void runOnlyInLeaderNode(final Runnable job, final String jobDescription) {
    ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(eos,
            HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
    Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
        @Override//from w w w .j av a2 s  . c  o  m
        public void onSuccess(Boolean isOwner) {
            if (isOwner) {
                job.run();
            } else {
                logger.trace("job is not run as i m not cluster owner desc :{} ", jobDescription);
            }
        }

        @Override
        public void onFailure(Throwable error) {
            logger.error("Failed to identity cluster owner ", error);
        }
    });
}

From source file:global.PlayAsync.java

public static <T> Future<T> asFuture(ListenableFuture<T> future) {
    final scala.concurrent.Promise<T> promise = new DefaultPromise<T>();
    Futures.addCallback(future, new FutureCallback<T>() {
        @Override/*from   ww  w  .j  a  va  2  s  . co m*/
        public void onSuccess(T result) {
            promise.success(result);
        }

        @Override
        public void onFailure(Throwable t) {
            promise.failure(t);
        }
    });
    return promise.future();
}