List of usage examples for com.google.common.util.concurrent Futures addCallback
public static <V> void addCallback(final ListenableFuture<V> future, final FutureCallback<? super V> callback, Executor executor)
From source file:de.cassandratraining.datastax.AsynchronousConsumer.java
protected void handle(QueryGenerator.Request request) { final TimerContext timerContext = reporter.latencies.time(); ResultSetFuture resultSetFuture = request.executeAsync(session); Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() { @Override// ww w.ja v a 2s.c om public void onSuccess(final ResultSet result) { timerContext.stop(); reporter.requests.mark(); request(); } @Override public void onFailure(final Throwable t) { // Could do better I suppose System.err.println("Error during request: " + t); timerContext.stop(); reporter.requests.mark(); request(); } }, executorService); }
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 o m*/ 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:io.mycat.manager.response.ReloadConfig.java
public static void execute(ManagerConnection c, final boolean loadAll) { // reload @@config_all ?? if (loadAll && !NIOProcessor.backends_old.isEmpty()) { c.writeErrMessage(ErrorCode.ER_YES, "The are several unfinished db transactions before executing \"reload @@config_all\", therefore the execution is terminated for logical integrity and please try again later."); return;//www . ja v a 2 s.c o m } final ReentrantLock lock = MycatServer.getInstance().getConfig().getLock(); lock.lock(); try { ListenableFuture<Boolean> listenableFuture = MycatServer.getInstance().getListeningExecutorService() .submit(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return loadAll ? reload_all() : reload(); } }); Futures.addCallback(listenableFuture, new ReloadCallBack(c), MycatServer.getInstance().getListeningExecutorService()); } finally { lock.unlock(); } }
From source file:org.opendaylight.controller.remote.rpc.RpcInvoker.java
@SuppressWarnings("checkstyle:IllegalCatch") private void executeRpc(final ExecuteRpc msg) { LOG.debug("Executing rpc {}", msg.getRpc()); final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc()); final ActorRef sender = getSender(); final ActorRef self = self(); final ListenableFuture<DOMRpcResult> future; try {/*from w ww . jav a2 s. c om*/ future = rpcService.invokeRpc(schemaPath, msg.getInputNormalizedNode()); } catch (final RuntimeException e) { LOG.debug("Failed to invoke RPC {}", msg.getRpc(), e); sender.tell(new akka.actor.Status.Failure(e), sender); return; } Futures.addCallback(future, new FutureCallback<DOMRpcResult>() { @Override public void onSuccess(final DOMRpcResult result) { if (result == null) { // This shouldn't happen but the FutureCallback annotates the result param with Nullable so // handle null here to avoid FindBugs warning. LOG.debug("Got null DOMRpcResult - sending null response for execute rpc : {}", msg.getRpc()); sender.tell(new RpcResponse(null), self); return; } if (!result.getErrors().isEmpty()) { final String message = String.format("Execution of RPC %s failed", msg.getRpc()); sender.tell(new akka.actor.Status.Failure(new RpcErrorsException(message, result.getErrors())), self); } else { LOG.debug("Sending response for execute rpc : {}", msg.getRpc()); sender.tell(new RpcResponse(result.getResult()), self); } } @Override public void onFailure(final Throwable failure) { LOG.debug("Failed to execute RPC {}", msg.getRpc(), failure); LOG.error("Failed to execute RPC {} due to {}. More details are available on DEBUG level.", msg.getRpc(), Throwables.getRootCause(failure)); sender.tell(new akka.actor.Status.Failure(failure), self); } }, MoreExecutors.directExecutor()); }
From source file:com.orangerhymelabs.helenus.cassandra.database.DatabaseService.java
public void update(Database database, FutureCallback<Database> callback) { Futures.addCallback(update(database), callback, MoreExecutors.directExecutor()); }
From source file:com.orangerhymelabs.helenus.cassandra.view.ViewService.java
public void read(String database, String table, String view, FutureCallback<View> callback) { Futures.addCallback(read(database, table, view), callback, MoreExecutors.directExecutor()); }
From source file:org.robotninjas.concurrent.FluentFutureTask.java
@Override public FluentFuture<V> addCallback(Executor executor, FutureCallback<V> callback) { Futures.addCallback(this, callback, executor); return this; }
From source file:info.archinnov.achilles.internal.async.AsyncUtils.java
public void maybeAddAsyncListeners(ListenableFuture<?> listenableFuture, Options options, ExecutorService executorService) { if (options.hasAsyncListeners()) { for (FutureCallback<Object> callback : options.getAsyncListeners()) { Futures.addCallback(listenableFuture, callback, executorService); }//from w ww .j a v a 2s .co m } }
From source file:com.tinspx.util.concurrent.FutureResult.java
@SuppressWarnings("LeakingThisInConstructor") private FutureResult(ListenableFuture<T> future) { Futures.addCallback(future, this, MoreExecutors.directExecutor()); }
From source file:org.apache.druid.client.cache.BackgroundCachePopulator.java
@Override public <T, CacheType> Sequence<T> wrap(final Sequence<T> sequence, final Function<T, CacheType> cacheFn, final Cache cache, final Cache.NamedKey cacheKey) { final List<ListenableFuture<CacheType>> cacheFutures = new ArrayList<>(); final Sequence<T> wrappedSequence = Sequences.map(sequence, input -> { cacheFutures.add(exec.submit(() -> cacheFn.apply(input))); return input; });//from ww w.j a va 2s. c o m return Sequences.withEffect(wrappedSequence, () -> { Futures.addCallback(Futures.allAsList(cacheFutures), new FutureCallback<List<CacheType>>() { @Override public void onSuccess(List<CacheType> results) { populateCache(cache, cacheKey, results); // Help out GC by making sure all references are gone cacheFutures.clear(); } @Override public void onFailure(Throwable t) { log.error(t, "Background caching failed"); } }, exec); }, MoreExecutors.sameThreadExecutor()); }