List of usage examples for com.google.common.util.concurrent Futures immediateFailedFuture
@CheckReturnValue public static <V> ListenableFuture<V> immediateFailedFuture(Throwable throwable)
From source file:com.proofpoint.event.collector.LocalEventClient.java
@Override public <T> ListenableFuture<Void> post(EventGenerator<T> eventGenerator) { checkNotNull(eventGenerator, "eventGenerator"); try {/*from ww w.ja va 2 s. c o m*/ eventGenerator.generate(new EventPoster<T>() { @Override public void post(T event) throws IOException { checkNotNull(event, "event"); for (EventWriter eventWriter : eventWriters) { eventWriter.write(serializeEvent(event)); } } }); } catch (IOException e) { return Futures.immediateFailedFuture(new RuntimeException(e)); } return Futures.immediateFuture(null); }
From source file:com.mypurecloud.sdk.v2.api.BillingApiAsync.java
/** * Get a report of the billable usages (e.g. licenses and devices utilized) for a given period. * /*w ww . j a v a2 s . co m*/ * @param request the request object * @param callback the action to perform when the request is completed * @return the future indication when the request has completed */ public Future<BillingUsageReport> getBillingReportsBillableusageAsync( GetBillingReportsBillableusageRequest request, final AsyncApiCallback<BillingUsageReport> callback) { try { final SettableFuture<BillingUsageReport> future = SettableFuture.create(); final boolean shouldThrowErrors = pcapiClient.getShouldThrowErrors(); pcapiClient.invokeAsync(request.withHttpInfo(), new TypeReference<BillingUsageReport>() { }, new AsyncApiCallback<ApiResponse<BillingUsageReport>>() { @Override public void onCompleted(ApiResponse<BillingUsageReport> response) { notifySuccess(future, callback, response.getBody()); } @Override public void onFailed(Throwable exception) { if (shouldThrowErrors) { notifyFailure(future, callback, exception); } else { notifySuccess(future, callback, null); } } }); return future; } catch (Throwable exception) { return Futures.immediateFailedFuture(exception); } }
From source file:co.cask.cdap.internal.app.runtime.AbstractProgramController.java
@Override public final ListenableFuture<ProgramController> suspend() { if (!state.compareAndSet(State.ALIVE, State.SUSPENDING)) { return Futures .immediateFailedFuture(new IllegalStateException("Suspension not allowed").fillInStackTrace()); }/*from www . ja va 2 s. c om*/ final SettableFuture<ProgramController> result = SettableFuture.create(); executor(State.SUSPENDING).execute(new Runnable() { @Override public void run() { try { caller.suspending(); doSuspend(); state.set(State.SUSPENDED); result.set(AbstractProgramController.this); caller.suspended(); } catch (Throwable t) { error(t, result); } } }); return result; }
From source file:io.v.x.jni.test.fortune.FortuneServerImpl.java
@Override public ListenableFuture<String> get(final VContext context, ServerCall call) { if (serverLatch != null) { serverLatch.countDown();//w ww .j av a 2 s .c o m } if (clientLatch != null) { try { // Caution: this is not idiomatic for server methods: they must be non-blocking. // However, it helps us with LameDuck tests. clientLatch.await(); } catch (InterruptedException e) { return Futures.immediateFailedFuture(new VException(e.getMessage())); } } if (lastAddedFortune == null) { return Futures.immediateFailedFuture(new NoFortunesException(context)); } return Futures.immediateFuture(lastAddedFortune); }
From source file:com.orangerhymelabs.helenus.cassandra.database.DatabaseService.java
private ListenableFuture<Database> update(Database database) { try {/* w ww .ja v a 2 s . co m*/ ValidationEngine.validateAndThrow(database); return databases.update(database); } catch (ValidationException e) { return Futures.immediateFailedFuture(e); } }
From source file:com.google.pubsub.clients.experimental.CPSSubscriberTask.java
@Override public ListenableFuture<RunResult> doRun() { synchronized (subscriber) { if (subscriber.isRunning()) { return Futures.immediateFuture(RunResult.empty()); }//from www.j a va 2 s. c o m try { subscriber.startAsync().awaitRunning(); } catch (Exception e) { log.error("Fatal error from subscriber.", e); return Futures.immediateFailedFuture(e); } return Futures.immediateFuture(RunResult.empty()); } }
From source file:com.facebook.buck.util.concurrent.WeightedListeningExecutorService.java
private <T> ListenableFuture<T> submitWithSemaphore(final Callable<T> callable, final ResourceAmounts amounts) { ListenableFuture<T> future = Futures.transformAsync(semaphore.acquire(amounts), input -> { try {// www . j a v a 2s.co m return Futures.immediateFuture(callable.call()); } catch (Throwable thrown) { return Futures.immediateFailedFuture(thrown); } }, delegate); future.addListener(() -> semaphore.release(amounts), com.google.common.util.concurrent.MoreExecutors.directExecutor()); return future; }
From source file:ch.raffael.util.swing.SwingUtil.java
public static <T> ListenableFuture<T> invokeInEventQueue(Callable<T> callable) { if (SwingUtilities.isEventDispatchThread()) { T result;//from ww w . j a va 2s. co m try { result = callable.call(); return Futures.immediateFuture(result); } catch (Exception e) { return Futures.immediateFailedFuture(e); } } else { ListenableFutureTask<T> future = ListenableFutureTask.create(callable); SwingUtilities.invokeLater(future); return future; } }
From source file:org.apache.twill.internal.Services.java
/** * Returns a {@link ListenableFuture} that will be completed when the given service is stopped. If the service * stopped due to error, the failure cause would be reflected in the future. * * @param service The {@link Service} to block on. * @return A {@link ListenableFuture} that will be completed when the service is stopped. *///from ww w .j av a 2 s .c om public static ListenableFuture<Service.State> getCompletionFuture(Service service) { final SettableFuture<Service.State> resultFuture = SettableFuture.create(); service.addListener(new ServiceListenerAdapter() { @Override public void terminated(Service.State from) { resultFuture.set(Service.State.TERMINATED); } @Override public void failed(Service.State from, Throwable failure) { resultFuture.setException(failure); } }, Threads.SAME_THREAD_EXECUTOR); Service.State state = service.state(); if (state == Service.State.TERMINATED) { return Futures.immediateFuture(state); } else if (state == Service.State.FAILED) { return Futures .immediateFailedFuture(new IllegalStateException("Service failed with unknown exception.")); } return resultFuture; }
From source file:io.joynr.messaging.http.operation.LongPollingMessageReceiver.java
@Override public synchronized Future<Void> start(MessageArrivedListener messageListener, ReceiverStatusListener... receiverStatusListeners) { synchronized (shutdownSynchronizer) { if (shutdown) { throw new JoynrShutdownException("Cannot register Message Listener: " + messageListener + ": LongPollingMessageReceiver is already shutting down"); }// w ww .j a v a2s. c om } if (isStarted()) { return Futures.immediateFailedFuture(new IllegalStateException("receiver is already started")); } final SettableFuture<Void> channelCreatedFuture = SettableFuture.create(); ReceiverStatusListener[] statusListeners = ObjectArrays.concat(new ReceiverStatusListener() { @Override // Register the ChannelUrl once the receiver is started public void receiverStarted() { if (channelMonitor.isChannelCreated()) { for (ChannelCreatedListener listener : channelCreatedListeners) { listener.channelCreated(channelMonitor.getChannelUrl()); } // Signal that the channel is now created for anyone blocking on the future channelCreatedFuture.set(null); } } @Override // Shutdown the receiver if an exception is thrown public void receiverException(Throwable e) { channelCreatedFuture.setException(e); channelMonitor.shutdown(); } }, receiverStatusListeners); channelMonitor.startLongPolling(messageListener, statusListeners); return channelCreatedFuture; }