List of usage examples for com.google.common.util.concurrent ListenableFuture get
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
From source file:io.greenbus.examples.Examples.java
/** * Java entry-point for running examples. * * Starts a client connection to GreenBus, logs in, and executes example code. * This is a "single shot" connection, if an application plans on running for extended periods it should use a * ConnectedApplicationManagers to be informed of the connection to the server is acquired or lost. * * @param args Command line arguments// w w w. j ava 2 s . com * @throws Exception */ public static void main(String[] args) throws Exception { // Load configuration files from paths provided in environment variables or in default locations final String configBaseDir = System.getProperty("io.greenbus.config.base", ""); final String amqpConfigPath = System.getProperty("io.greenbus.config.amqp", configBaseDir + "io.greenbus.msg.amqp.cfg"); final String userConfigPath = System.getProperty("io.greenbus.config.user", configBaseDir + "io.greenbus.user.cfg"); // Load broker settings from config file final AmqpSettings amqpSettings = new AmqpSettings(amqpConfigPath); // Load user settings (login credentials) from config file final UserSettings userSettings = UserSettings.load(userConfigPath); // Create ServiceConnection to the Qpid broker final ServiceConnection connection = ServiceConnectionFactory.create(amqpSettings, QpidBroker.instance(), 10000); // Get a Session object that has a valid auth token. Causes a service call to login final ListenableFuture<Session> loginFuture = connection.login(userSettings.user(), userSettings.password()); final Session session = loginFuture.get(5000, TimeUnit.MILLISECONDS); try { // Run Examples runAllExamples(session); } finally { // Disconnect from AMQP and shut down thread pools connection.disconnect(); } System.exit(0); }
From source file:co.cask.cdap.common.service.Services.java
/** * Attempts to start the passed in service * @param service The service to start// ww w. ja va2s. c om * @param timeout The duration to wait for the service to start * @param timeoutUnit The time unit used for the timeout parameter * @param timeoutErrorMessage An optional error message to display if starting the service times out * @throws TimeoutException If the service can not be started before the specified timeout * @throws InterruptedException If the service is interrupted while trying to start the service * @throws ExecutionException If an exception occurs while trying to start the service */ public static void startAndWait(Service service, long timeout, TimeUnit timeoutUnit, @Nullable String timeoutErrorMessage) throws TimeoutException, InterruptedException, ExecutionException { ListenableFuture<Service.State> startFuture = service.start(); try { startFuture.get(timeout, timeoutUnit); } catch (TimeoutException e) { LOG.error(timeoutErrorMessage != null ? timeoutErrorMessage : "Timeout while waiting to start service.", e); TimeoutException timeoutException = new TimeoutException(timeoutErrorMessage); if (e.getStackTrace() != null) { timeoutException.setStackTrace(e.getStackTrace()); } try { service.stop(); } catch (Exception stopException) { LOG.error("Error while trying to stop service: ", stopException); } throw timeoutException; } catch (InterruptedException e) { LOG.error("Interrupted while waiting to start service.", e); try { service.stop(); } catch (Exception stopException) { LOG.error("Error while trying to stop service:", stopException); } throw e; } }
From source file:net.devh.boot.grpc.test.util.FutureAssertions.java
@SuppressWarnings("unchecked") public static <T extends Exception> T assertFutureThrows(final Class<T> expectedType, final ListenableFuture<?> future, final int timeout, final TimeUnit timeoutUnit) { final Throwable cause = assertThrows(ExecutionException.class, () -> future.get(timeout, timeoutUnit)) .getCause();//from w w w . j a v a 2s. com final Class<? extends Throwable> causeClass = cause.getClass(); assertTrue(expectedType.isAssignableFrom(causeClass), "The cause was of type: " + causeClass.getName() + ", but it was expected to be a subclass of " + expectedType.getName()); return (T) cause; }
From source file:com.spotify.helios.testing.Jobs.java
static <T> T get(final ListenableFuture<T> future, final long timeout) throws InterruptedException, ExecutionException, TimeoutException { return future.get(timeout, MILLISECONDS); }
From source file:net.devh.boot.grpc.test.util.FutureAssertions.java
public static <T, R> void assertFutureEquals(final T expected, final ListenableFuture<R> future, final Function<R, T> unwrapper, final int timeout, final TimeUnit timeoutUnit) { try {//from w ww.j av a2s .c o m assertEquals(expected, unwrapper.apply(future.get(timeout, timeoutUnit))); } catch (InterruptedException | ExecutionException | TimeoutException e) { fail(e); } }
From source file:org.opendaylight.infrautils.utils.concurrent.ListenableFutures.java
public static <V, E extends Exception> V checkedGet(ListenableFuture<V> future, Function<? super Exception, E> mapper, long timeout, TimeUnit unit) throws E, TimeoutException { try {//w w w . j a va 2s . c o m return future.get(timeout, unit); // as in com.google.common.util.concurrent.AbstractCheckedFuture.checkedGet: } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw mapper.apply(e); } catch (CancellationException e) { throw mapper.apply(e); } catch (ExecutionException e) { throw mapper.apply(e); } }
From source file:com.android.tools.idea.run.LaunchTaskRunner.java
@Nullable private static IDevice waitForDevice(@NotNull ListenableFuture<IDevice> deviceFuture, @NotNull ProgressIndicator indicator, @NotNull LaunchStatus launchStatus) { while (true) { try {// www .j a v a2 s. c om return deviceFuture.get(1, TimeUnit.SECONDS); } catch (TimeoutException ignored) { } catch (InterruptedException e) { launchStatus.terminateLaunch("Interrupted while waiting for device"); return null; } catch (ExecutionException e) { launchStatus.terminateLaunch("Error while waiting for device: " + e.getCause().getMessage()); return null; } if (indicator.isCanceled()) { launchStatus.terminateLaunch("User cancelled launch"); return null; } if (launchStatus.isLaunchTerminated()) { return null; } } }
From source file:co.paralleluniverse.fibers.futures.AsyncListenableFuture.java
/** * Blocks the current strand (either fiber or thread) until the given future completes - but no longer than the given timeout - and returns its result. * * @param future the future//from w ww. java2 s . c o m * @param timeout the maximum duration to wait for the future's result * @param unit the timeout's time unit * @return the future's result * @throws ExecutionException if the future's computation threw an exception * @throws TimeoutException if the timeout expired before the future completed * @throws InterruptedException if the current thread was interrupted while waiting */ public static <V> V get(ListenableFuture<V> future, long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, SuspendExecution, TimeoutException { if (Fiber.isCurrentFiber() && !future.isDone()) return new AsyncListenableFuture<>(future).run(timeout, unit); else return future.get(timeout, unit); }
From source file:net.floodlightcontroller.core.web.SwitchRoleResource.java
private static OFRoleReply setSwitchRole(IOFSwitch sw, OFControllerRole role) { try {//from w w w . j a va2s .c o m if (sw.getOFFactory().getVersion().compareTo(OFVersion.OF_12) < 0) { OFNiciraControllerRole nrole; switch (role) { case ROLE_EQUAL: nrole = OFNiciraControllerRole.ROLE_OTHER; log.warn("Assuming EQUAL as OTHER for Nicira role request."); break; case ROLE_MASTER: nrole = OFNiciraControllerRole.ROLE_MASTER; break; case ROLE_SLAVE: nrole = OFNiciraControllerRole.ROLE_SLAVE; break; case ROLE_NOCHANGE: log.error("Nicira extension does not support NOCHANGE role. Thus, we won't change the role."); return OFFactories.getFactory(OFVersion.OF_13).buildRoleReply().setRole(sw.getControllerRole()) .setGenerationId(U64.ZERO).build(); default: log.error("Impossible to have anything other than MASTER, OTHER, or SLAVE for Nicira role."); return OFFactories.getFactory(OFVersion.OF_13).buildRoleReply().setRole(sw.getControllerRole()) .setGenerationId(U64.ZERO).build(); } ListenableFuture<OFNiciraControllerRoleReply> future = sw .writeRequest(sw.getOFFactory().buildNiciraControllerRoleRequest().setRole(nrole).build()); OFNiciraControllerRoleReply nreply = future.get(10, TimeUnit.SECONDS); if (nreply != null) { /* Turn the OFControllerRoleReply into a OFNiciraControllerRoleReply */ switch (nreply.getRole()) { case ROLE_MASTER: return OFFactories.getFactory(OFVersion.OF_13).buildRoleReply() .setRole(OFControllerRole.ROLE_MASTER).setGenerationId(U64.ZERO).build(); case ROLE_OTHER: return OFFactories.getFactory(OFVersion.OF_13).buildRoleReply() .setRole(OFControllerRole.ROLE_EQUAL).setGenerationId(U64.ZERO).build(); case ROLE_SLAVE: return OFFactories.getFactory(OFVersion.OF_13).buildRoleReply() .setRole(OFControllerRole.ROLE_SLAVE).setGenerationId(U64.ZERO).build(); default: log.error( "Impossible to have anything other than MASTER, OTHER, or SLAVE for Nicira role: {}.", nreply.getRole().toString()); break; } } else { log.error("Did not receive Nicira role reply for switch {}.", sw.getId().toString()); } } else { ListenableFuture<OFRoleReply> future = sw.writeRequest( sw.getOFFactory().buildRoleRequest().setGenerationId(U64.ZERO).setRole(role).build()); return future.get(10, TimeUnit.SECONDS); } } catch (Exception e) { log.error("Failure setting switch {} role to {}.", sw.toString(), role.toString()); log.error(e.getMessage()); } return null; }
From source file:com.magnet.yak.command.AppCreate.java
public AppCreateOutput exec(AppCreateInput input) { LOGGER.trace("exec : input={}", input); try {//from ww w . j ava2s. c o m ListenableFuture<AppCreateOutput> future = apply(input); return future.get(waitTime, TimeUnit.SECONDS); } catch (Exception e) { e.printStackTrace(); } return new AppCreateOutput(); }