Example usage for com.google.common.util.concurrent Service awaitTerminated

List of usage examples for com.google.common.util.concurrent Service awaitTerminated

Introduction

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

Prototype

void awaitTerminated(long timeout, TimeUnit unit) throws TimeoutException;

Source Link

Document

Waits for the Service to reach a terminal state (either Service.State#TERMINATED terminated or Service.State#FAILED failed ) for no more than the given time.

Usage

From source file:io.pravega.segmentstore.server.ServiceListeners.java

/**
 * Awaits for the given Service to shut down, whether normally or exceptionally.
 *
 * @param service       The store to monitor.
 * @param timeout       The maximum amount of time to wait for the shutdown.
 * @param throwIfFailed Throw the resulting exception if the store ended up in a FAILED state.
 * @throws TimeoutException If the store did not shut down within the specified amount of time. This exception is
 *                          thrown regardless of the value passed in as throwIfFailed.
 */// w w  w . ja  va2 s .c  om
public static void awaitShutdown(Service service, Duration timeout, boolean throwIfFailed)
        throws TimeoutException {
    try {
        service.awaitTerminated(timeout.toMillis(), TimeUnit.MILLISECONDS);
    } catch (IllegalStateException ex) {
        if (throwIfFailed || service.state() != Service.State.FAILED) {
            throw ex;
        }
    }
}

From source file:co.runrightfast.core.utils.ServiceUtils.java

/**
 * Logs a warning every 10 seconds, waiting for the service to stop
 *
 * @param service Service/*from  w  w  w.java  2  s . c om*/
 */
static void awaitTerminated(@NonNull final Service service) {
    while (true) {
        try {
            service.awaitTerminated(10, TimeUnit.SECONDS);
            return;
        } catch (final TimeoutException ex) {
            LOG.logp(WARNING, ServiceUtils.class.getName(), "awaitTerminated",
                    "Wating for service to terminate : {0}", service.getClass().getName());
        }
    }
}

From source file:co.runrightfast.commons.utils.ServiceUtils.java

/**
 * Logs a warning every 10 seconds, waiting for the service to stop
 *
 * @param service Service//from www.j  a v a  2 s .  c o  m
 */
public static void awaitTerminated(@NonNull final Service service) {
    for (int i = 1; true; i++) {
        try {
            service.awaitTerminated(10, TimeUnit.SECONDS);
            return;
        } catch (final TimeoutException ex) {
            final int elapsedTimeSeconds = i * 10;
            log.warn(
                    String.format("Wating for service to terminate : %s : %d seconds",
                            service.getClass().getName(), elapsedTimeSeconds),
                    new ApplicationException(MAJOR, ex));
        }
    }
}

From source file:io.pravega.common.concurrent.ServiceShutdownListener.java

/**
 * Awaits for the given Services to shut down, whether normally or exceptionally.
 *
 * @param services      The services to monitor.
 * @param timeout       Timeout for the operation.
 * @param throwIfFailed Throw an IllegalStateException if any of the services ended up in a FAILED state.
 * @param <T>           The type of the Collection's elements.
 * @throws TimeoutException If the store did not shut down within the specified amount of time. This exception is
 *                          thrown regardless of the value passed in as throwIfFailed.
 */// w w w  .j  av  a  2  s .  c o m
public static <T extends Service> void awaitShutdown(Collection<T> services, Duration timeout,
        boolean throwIfFailed) throws TimeoutException {
    TimeoutTimer timer = new TimeoutTimer(timeout);
    int failureCount = 0;
    for (Service service : services) {
        try {
            // We wait on each store individually, making sure we decrease the available timeout. It does not matter
            // if we do it sequentially or in parallel - our contract is to wait for all services within a timeout;
            // if any of the services did not respond within the given time, it is a reason for failure.
            service.awaitTerminated(timer.getRemaining().toMillis(), TimeUnit.MILLISECONDS);
        } catch (IllegalStateException ex) {
            if (throwIfFailed || service.state() != Service.State.FAILED) {
                failureCount++;
            }
        }
    }

    if (failureCount > 0) {
        throw new IllegalStateException(String.format("%d store(s) could not be shut down.", failureCount));
    }
}

From source file:gobblin.broker.DefaultBrokerCache.java

/**
 * Invalidate all objects at scopes which are descendant of the input scope. Any such invalidated object that is a
 * {@link Closeable} will be closed, and any such object which is a {@link Service} will be shutdown.
 * @throws IOException/*  w  ww  .  j a  v  a 2  s.c o  m*/
 */
public void close(ScopeWrapper<S> scope) throws IOException {
    List<Service> awaitShutdown = Lists.newArrayList();

    for (Map.Entry<RawJobBrokerKey, Object> entry : Maps
            .filterKeys(this.sharedResourceCache.asMap(), new ScopeIsAncestorFilter(scope)).entrySet()) {

        this.sharedResourceCache.invalidate(entry.getKey());

        if (entry.getValue() instanceof ResourceInstance) {
            Object obj = ((ResourceInstance) entry.getValue()).getResource();

            SharedResourcesBrokerUtils.shutdownObject(obj, log);
            if (obj instanceof Service) {
                awaitShutdown.add((Service) obj);
            }
        }
    }

    for (Service service : awaitShutdown) {
        try {
            service.awaitTerminated(10, TimeUnit.SECONDS);
        } catch (TimeoutException te) {
            log.error("Failed to shutdown {}.", service);
        }
    }
}

From source file:org.apache.gobblin.broker.DefaultBrokerCache.java

/**
 * Invalidate all objects at scopes which are descendant of the input scope. Any such invalidated object that is a
 * {@link Closeable} will be closed, and any such object which is a {@link Service} will be shutdown.
 * @throws IOException//from  w  w w  . j  av  a  2  s  .  c  o  m
 */
public void close(ScopeWrapper<S> scope) throws IOException {
    List<Throwable> exceptionsList = Lists.newArrayList();
    List<Service> awaitShutdown = Lists.newArrayList();

    for (Map.Entry<RawJobBrokerKey, Object> entry : Maps
            .filterKeys(this.sharedResourceCache.asMap(), new ScopeIsAncestorFilter(scope)).entrySet()) {

        this.sharedResourceCache.invalidate(entry.getKey());

        if (entry.getValue() instanceof ResourceInstance) {
            Object obj = ((ResourceInstance) entry.getValue()).getResource();
            // Catch unchecked exception while closing resources, make sure all resources managed by cache are closed.
            try {
                SharedResourcesBrokerUtils.shutdownObject(obj, log);
            } catch (Throwable t) {
                exceptionsList.add(t);
            }
            if (obj instanceof Service) {
                awaitShutdown.add((Service) obj);
            }
        }
    }

    for (Service service : awaitShutdown) {
        try {
            service.awaitTerminated(10, TimeUnit.SECONDS);
        } catch (TimeoutException te) {
            log.error("Failed to shutdown {}.", service);
        }
    }

    // log exceptions while closing resources up.
    if (exceptionsList.size() > 0) {
        log.error(exceptionsList.stream().map(Throwables::getStackTraceAsString)
                .collect(Collectors.joining("\n")));
    }
}