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

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

Introduction

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

Prototype

State state();

Source Link

Document

Returns the lifecycle state of the service.

Usage

From source file:org.invenzzia.helium.gui.events.ContextStateChangeEvent.java

public ContextStateChangeEvent(AbstractContext context, Service.State state) {
    super(context, state);
}

From source file:org.invenzzia.helium.behaviors.event.StateChangeEvent.java

public StateChangeEvent(ILifecycle lifecycleObject, Service.State state) {
    this.lifecycleObject = Preconditions.checkNotNull(lifecycleObject);
    this.state = Preconditions.checkNotNull(state);
}

From source file:c5db.interfaces.server.ModuleStateChange.java

public ModuleStateChange(C5Module module, Service.State state) {
    this.module = module;
    this.state = state;
}

From source file:co.cask.cdap.common.service.Services.java

/**
 * Attempts to start the passed in service
 * @param service The service to start/*from www  .j  a v  a2  s  .  co m*/
 * @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:com.griddynamics.jagger.master.CompositeTaskDistributor.java

@Override
public Service distribute(final ExecutorService executor, final String sessionId, final String taskId,
        final Multimap<NodeType, NodeId> availableNodes, final Coordinator coordinator,
        final CompositeTask task, final DistributionListener listener) {
    log.debug("Composite task {} with id {} distribute configuration started", task, taskId);

    Function<CompositableTask, Service> convertToRunnable = new Function<CompositableTask, Service>() {
        @Override/*from  w w  w .j  a  va 2 s.c om*/
        @SuppressWarnings("unchecked")
        public Service apply(CompositableTask task) {
            TaskDistributor taskDistributor = distributorRegistry.getTaskDistributor(task.getClass());
            task.setParentTaskId(taskId);
            String childTaskId = taskIdProvider.getTaskId();
            return taskDistributor.distribute(executor, sessionId, childTaskId, availableNodes, coordinator,
                    task, listener);
        }
    };

    final List<Service> leading = Lists.newLinkedList(Lists.transform(task.getLeading(), convertToRunnable));
    final List<Service> attendant = Lists
            .newLinkedList(Lists.transform(task.getAttendant(), convertToRunnable));

    return new AbstractDistributionService(executor) {

        final List<Future<State>> leadingTerminateFutures = Lists.newLinkedList();

        @Override
        protected void run() throws Exception {

            List<Future<State>> futures = Lists.newLinkedList();
            for (Service service : Iterables.concat(leading, attendant)) {
                ListenableFuture<State> future = service.start();
                futures.add(future);
            }

            for (Future<State> future : futures) {
                State state = Futures.get(future, timeoutsConfiguration.getWorkloadStartTimeout());
                log.debug("Service started with state {}", state);
            }

            while (isRunning()) {
                if (activeLeadingTasks() == 0) {
                    break;
                }

                TimeUtils.sleepMillis(500);
            }
        }

        private int activeLeadingTasks() {
            int result = 0;

            Iterator<Service> it = leading.iterator();
            while (it.hasNext()) {
                Service service = it.next();
                if (service.state() == State.TERMINATED || service.state() == State.FAILED) {
                    log.debug("State {}", service.state());
                    leadingTerminateFutures.addAll(requestTermination(Collections.singleton(service), true));
                    it.remove();
                } else {
                    result++;
                }
            }

            return result;
        }

        @Override
        protected void shutDown() throws Exception {
            stopAll();
            super.shutDown();
        }

        private void awaitLeading(List<Future<State>> leadingFutures) {
            for (Future<State> future : leadingFutures) {
                Futures.get(future, timeoutsConfiguration.getWorkloadStopTimeout());
            }
        }

        private List<Future<State>> requestTermination(Iterable<Service> services, boolean leading) {
            List<Future<State>> leadingFutures = Lists.newLinkedList();
            for (Service service : services) {
                if (service.state() == State.FAILED) {
                    if (leading) {
                        throw new IllegalStateException("Failed to run child distributor: " + service);
                    } else {
                        log.warn("Attendant service {} failed", service);
                        continue;
                    }
                }

                leadingFutures.add(service.stop());
            }
            return leadingFutures;
        }

        private void stopAll() {
            List<Future<State>> leadingFutures = requestTermination(leading, true);
            List<Future<State>> attendantFutures = requestTermination(attendant, false);
            leadingFutures.addAll(leadingTerminateFutures);

            awaitLeading(leadingFutures);
            awaitAttendant(attendantFutures);
        }

        private void awaitAttendant(List<Future<State>> attendantFutures) {
            for (Future<State> future : attendantFutures) {
                try {
                    future.get(timeoutsConfiguration.getWorkloadStopTimeout(), TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    log.warn("Attendant task timeout", e);
                } catch (InterruptedException e) {
                    log.warn("Interrupted", e);
                } catch (ExecutionException e) {
                    log.warn("Attendant task failed", e);
                }
            }
        }

        @Override
        public String toString() {
            return CompositeTaskDistributor.class.getName() + " distributor";
        }
    };
}

From source file:co.cask.cdap.app.runtime.workflow.WorkflowStatus.java

public Service.State getState() {
    return state;
}

From source file:org.apache.twill.common.ServiceListenerAdapter.java

@Override
public void stopping(Service.State from) {
    // No-op
}

From source file:org.graylog2.utilities.LatchUpdaterListener.java

@Override
public void failed(Service.State from, Throwable failure) {
    latch.countDown();
}

From source file:com.continuuity.weave.internal.state.StateNode.java

/**
 * Constructs a StateNode with {@link ServiceController.State#FAILED} caused by the given error.
 *///from  www.  j  a  v a 2s  .c  o m
public StateNode(Throwable error) {
    this(Service.State.FAILED, error.getMessage(), error.getStackTrace());
}

From source file:org.apache.twill.common.ServiceListenerAdapter.java

@Override
public void terminated(Service.State from) {
    // No-op
}