Home | History | Annotate | Download | only in concurrent

Lines Matching refs:service

26 import static com.google.common.util.concurrent.Service.State.FAILED;
27 import static com.google.common.util.concurrent.Service.State.NEW;
28 import static com.google.common.util.concurrent.Service.State.RUNNING;
29 import static com.google.common.util.concurrent.Service.State.STARTING;
30 import static com.google.common.util.concurrent.Service.State.STOPPING;
31 import static com.google.common.util.concurrent.Service.State.TERMINATED;
54 import com.google.common.util.concurrent.Service.State;
74 * A manager for monitoring and controlling a set of {@linkplain Service services}. This class
76 * {@linkplain #servicesByState inspecting} a collection of {@linkplain Service services}.
80 * <p>While it is recommended that service lifecycles be managed via this class, state transitions
89 * Set<Service> services = ...;
96 * public void failure(Service service) {
120 * service failure and to ensure that when the JVM is shutting down all the services are stopped.
142 * Service service} starts, or terminates), should attach {@linkplain Service.Listener service
143 * listeners} to each individual service.
151 * Called when the service initially becomes healthy.
167 * Called when a component service has {@linkplain State#FAILED failed}.
169 * @param service The service that failed.
171 public void failure(Service service) {}
176 * service listeners}. This is extracted into its own object so that {@link ServiceListener}
182 private final ImmutableList<Service> services;
192 public ServiceManager(Iterable<? extends Service> services) {
193 ImmutableList<Service> copy = ImmutableList.copyOf(services);
196 // fired. To avoid this we substitute a placeholder service.
200 copy = ImmutableList.<Service>of(new NoOpService());
206 for (Service service : copy) {
207 service.addListener(new ServiceListener(service, stateReference), directExecutor());
209 // to a NEW service.
210 checkArgument(service.state() == NEW, "Can only manage NEW services, %s", service);
221 * {@linkplain Service#startAsync started}.
225 * invoked in the same order as the underlying service enters those states. Additionally, at most
247 * added before any of the managed services are {@linkplain Service#startAsync started}.
251 * invoked in the same order as the underlying service enters those states. Additionally, at most
265 * Initiates service {@linkplain Service#startAsync startup} on all the services being managed.
273 for (Service service : services) {
274 State state = service.state();
275 checkState(state == NEW, "Service %s is %s, cannot start it.", service, state);
277 for (Service service : services) {
279 state.tryStartTiming(service);
280 service.startAsync();
282 // This can happen if the service has already been started or stopped (e.g. by another
283 // service or listener). Our contract says it is safe to call this method if
286 logger.log(Level.WARNING, "Unable to start Service " + service, e);
297 * @throws IllegalStateException if the service manager reaches a state from which it cannot
312 * @throws IllegalStateException if the service manager reaches a state from which it cannot
320 * Initiates service {@linkplain Service#stopAsync shutdown} if necessary on all the services
326 for (Service service : services) {
327 service.stopAsync();
334 * services will either be {@linkplain Service.State#TERMINATED terminated} or {@linkplain
335 * Service.State#FAILED failed}.
343 * this method returns all services will either be {@linkplain Service.State#TERMINATED
344 * terminated} or {@linkplain Service.State#FAILED failed}.
361 for (Service service : services) {
362 if (!service.isRunning()) {
375 public ImmutableMultimap<State, Service> servicesByState() {
380 * Returns the service load times. This value will only return startup times for services that
386 public ImmutableMap<Service, Long> startupTimes() {
404 final SetMultimap<State, Service> servicesByState =
405 Multimaps.newSetMultimap(new EnumMap<State, Collection<Service>>(State.class),
406 new Supplier<Set<Service>>() {
407 @Override public Set<Service> get() {
416 final Map<Service, Stopwatch> startupTimers = Maps.newIdentityHashMap();
426 * to any service performing a transition, then we can fail in the ServiceManager constructor
427 * rather than in a Service.Listener callback.
443 // All services have started or some service has terminated/failed.
471 ServiceManagerState(ImmutableCollection<Service> services) {
477 * Attempts to start the timer immediately prior to the service being started via
478 * {@link Service#startAsync()}.
480 void tryStartTiming(Service service) {
483 Stopwatch stopwatch = startupTimers.get(service);
485 startupTimers.put(service, Stopwatch.createStarted());
504 List<Service> servicesInBadStates = Lists.newArrayList();
505 for (Service service : servicesByState().values()) {
506 if (service.state() != NEW) {
507 servicesInBadStates.add(service);
574 ImmutableMultimap<State, Service> servicesByState() {
575 ImmutableSetMultimap.Builder<State, Service> builder = ImmutableSetMultimap.builder();
578 for (Entry<State, Service> entry : servicesByState.entries()) {
589 ImmutableMap<Service, Long> startupTimes() {
590 List<Entry<Service, Long>> loadTimes;
594 // N.B. There will only be an entry in the map if the service has started
595 for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) {
596 Service service = entry.getKey();
598 if (!stopWatch.isRunning() && !(service instanceof NoOpService)) {
599 loadTimes.add(Maps.immutableEntry(service, stopWatch.elapsed(MILLISECONDS)));
606 .onResultOf(new Function<Entry<Service, Long>, Long>() {
607 @Override public Long apply(Map.Entry<Service, Long> input) {
611 ImmutableMap.Builder<Service, Long> builder = ImmutableMap.builder();
612 for (Entry<Service, Long> entry : loadTimes) {
619 * Updates the state with the given service transition.
629 void transitionService(final Service service, State from, State to) {
630 checkNotNull(service);
639 checkState(servicesByState.remove(from, service),
640 "Service %s not at the expected location in the state map %s", service, from);
641 checkState(servicesByState.put(to, service),
642 "Service %s in the state map unexpectedly at %s", service, to);
644 Stopwatch stopwatch = startupTimers.get(service);
646 // This means the service was started by some means other than ServiceManager.startAsync
648 startupTimers.put(service, stopwatch);
653 if (!(service instanceof NoOpService)) {
654 logger.log(Level.FINE, "Started {0} in {1}.", new Object[] {service, stopwatch});
659 // Did a service fail?
661 fireFailedListeners(service);
666 // they are not guaranteed to get 'true', because any service could fail right now.
689 void fireFailedListeners(final Service service) {
690 new Callback<Listener>("failed({service=" + service + "})") {
692 listener.failure(service);
719 * A {@link Service} that wraps another service and times how long it takes for it to start and
720 * also calls the {@link ServiceManagerState#transitionService(Service, State, State)},
723 private static final class ServiceListener extends Service.Listener {
724 final Service service;
729 ServiceListener(Service service, WeakReference<ServiceManagerState> state) {
730 this.service = service;
737 state.transitionService(service, NEW, STARTING);
738 if (!(service instanceof NoOpService)) {
739 logger.log(Level.FINE, "Starting {0}.", service);
747 state.transitionService(service, STARTING, RUNNING);
754 state.transitionService(service, from, STOPPING);
761 if (!(service instanceof NoOpService)) {
762 logger.log(Level.FINE, "Service {0} has terminated. Previous state was: {1}",
763 new Object[] {service, from});
765 state.transitionService(service, from, TERMINATED);
774 if (!(service instanceof NoOpService)) {
775 logger.log(Level.SEVERE, "Service " + service + " has failed in the " + from + " state.",
778 state.transitionService(service, from, FAILED);
784 * A {@link Service} instance that does nothing. This is only useful as a placeholder to