001    /*
002     * Copyright (C) 2009 Google Inc.
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.Service;
021    import com.google.common.base.Throwables;
022    
023    import java.util.concurrent.Executor;
024    import java.util.concurrent.Future;
025    
026    /**
027     * Base class for services that can implement {@link #startUp}, {@link #run} and
028     * {@link #shutDown} methods. This class uses a single thread to execute the
029     * service; consider {@link AbstractService} if you would like to manage any
030     * threading manually.
031     *
032     * @author Jesse Wilson
033     * @since 1
034     */
035    @Beta
036    public abstract class AbstractExecutionThreadService implements Service {
037    
038      /* use AbstractService for state management */
039      private final Service delegate = new AbstractService() {
040        @Override protected final void doStart() {
041          executor().execute(new Runnable() {
042            public void run() {
043              try {
044                startUp();
045                notifyStarted();
046    
047                if (isRunning()) {
048                  try {
049                    AbstractExecutionThreadService.this.run();
050                  } catch (Throwable t) {
051                    try {
052                      shutDown();
053                    } catch (Exception ignored) {}
054                    throw t;
055                  }
056                }
057    
058                shutDown();
059                notifyStopped();
060              } catch (Throwable t) {
061                notifyFailed(t);
062                throw Throwables.propagate(t);
063              }
064            }
065          });
066        }
067    
068        @Override protected void doStop() {
069          triggerShutdown();
070        }
071      };
072    
073      /**
074       * Start the service. This method is invoked on the execution thread.
075       */
076      protected void startUp() throws Exception {}
077    
078      /**
079       * Run the service. This method is invoked on the execution thread.
080       * Implementations must respond to stop requests. You could poll for lifecycle
081       * changes in a work loop:
082       * <pre>
083       *   public void run() {
084       *     while ({@link #isRunning()}) {
085       *       // perform a unit of work
086       *     }
087       *   }
088       * </pre>
089       * ...or you could respond to stop requests by implementing {@link
090       * #triggerShutdown()}, which should cause {@link #run()} to return.
091       */
092      protected abstract void run() throws Exception;
093    
094      /**
095       * Stop the service. This method is invoked on the execution thread.
096       */
097      // TODO: consider supporting a TearDownTestCase-like API
098      protected void shutDown() throws Exception {}
099    
100      /**
101       * Invoked to request the service to stop.
102       */
103      protected void triggerShutdown() {}
104    
105      /**
106       * Returns the {@link Executor} that will be used to run this service.
107       * Subclasses may override this method to use a custom {@link Executor}, which
108       * may configure its worker thread with a specific name, thread group or
109       * priority. The returned executor's {@link Executor#execute(Runnable)
110       * execute()} method is called when this service is started, and should return
111       * promptly.
112       */
113      protected Executor executor() {
114        return new Executor() {
115          public void execute(Runnable command) {
116            new Thread(command, getServiceName()).start();
117          }
118        };
119      }
120    
121      @Override public String toString() {
122        return getServiceName() + " [" + state() + "]";
123      }
124    
125      // We override instead of using ForwardingService so that these can be final.
126    
127      @Override public final Future<State> start() {
128        return delegate.start();
129      }
130    
131      @Override public final State startAndWait() {
132        return delegate.startAndWait();
133      }
134    
135      @Override public final boolean isRunning() {
136        return delegate.isRunning();
137      }
138    
139      @Override public final State state() {
140        return delegate.state();
141      }
142    
143      @Override public final Future<State> stop() {
144        return delegate.stop();
145      }
146    
147      @Override public final State stopAndWait() {
148        return delegate.stopAndWait();
149      }
150    
151      private String getServiceName() {
152        return getClass().getSimpleName();
153      }
154    }