001 /*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package com.google.common.util.concurrent;
018
019 import com.google.common.annotations.Beta;
020 import com.google.common.base.Throwables;
021
022 import java.util.concurrent.Executor;
023
024 /**
025 * Base class for services that do not need a thread while "running"
026 * but may need one during startup and shutdown. Subclasses can
027 * implement {@link #startUp} and {@link #shutDown} methods, each
028 * which run in a executor which by default uses a separate thread
029 * for each method.
030 *
031 * @author Chris Nokleberg
032 * @since 1.0
033 */
034 @Beta
035 public abstract class AbstractIdleService implements Service {
036
037 /* use AbstractService for state management */
038 private final Service delegate = new AbstractService() {
039
040 @Override
041 protected final void doStart() {
042 executor(State.STARTING).execute(new Runnable() {
043 public void run() {
044 try {
045 startUp();
046 notifyStarted();
047 } catch (Throwable t) {
048 notifyFailed(t);
049 throw Throwables.propagate(t);
050 }
051 }
052 });
053 }
054
055
056 @Override
057 protected final void doStop() {
058 executor(State.STOPPING).execute(new Runnable() {
059 public void run() {
060 try {
061 shutDown();
062 notifyStopped();
063 } catch (Throwable t) {
064 notifyFailed(t);
065 throw Throwables.propagate(t);
066 }
067 }
068 });
069 }
070 };
071
072 /** Start the service. */
073 protected abstract void startUp() throws Exception;
074
075 /** Stop the service. */
076 protected abstract void shutDown() throws Exception;
077
078 /**
079 * Returns the {@link Executor} that will be used to run this service.
080 * Subclasses may override this method to use a custom {@link Executor}, which
081 * may configure its worker thread with a specific name, thread group or
082 * priority. The returned executor's {@link Executor#execute(Runnable)
083 * execute()} method is called when this service is started and stopped,
084 * and should return promptly.
085 *
086 * @param state {@link Service.State#STARTING} or
087 * {@link Service.State#STOPPING}, used by the default implementation for
088 * naming the thread
089 */
090 protected Executor executor(final State state) {
091 return new Executor() {
092 public void execute(Runnable command) {
093 new Thread(command, getServiceName() + " " + state).start();
094 }
095 };
096 }
097
098
099 @Override
100 public String toString() {
101 return getServiceName() + " [" + state() + "]";
102 }
103
104 // We override instead of using ForwardingService so that these can be final.
105
106 public final ListenableFuture<State> start() {
107 return delegate.start();
108 }
109
110 public final State startAndWait() {
111 return delegate.startAndWait();
112 }
113
114 public final boolean isRunning() {
115 return delegate.isRunning();
116 }
117
118 public final State state() {
119 return delegate.state();
120 }
121
122 public final ListenableFuture<State> stop() {
123 return delegate.stop();
124 }
125
126 public final State stopAndWait() {
127 return delegate.stopAndWait();
128 }
129
130 public final void addListener(Listener listener, Executor executor) {
131 delegate.addListener(listener, executor);
132 }
133
134 private String getServiceName() {
135 return getClass().getSimpleName();
136 }
137 }