Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.util.concurrent;
 
 
Base class for services that can implement startUp(), run() and shutDown() methods. This class uses a single thread to execute the service; consider AbstractService if you would like to manage any threading manually.

Author(s):
Jesse Wilson
Since:
1.0
 
 public abstract class AbstractExecutionThreadService implements Service {
   private static final Logger logger = Logger.getLogger(
 
   /* use AbstractService for state management */
   private final Service delegate = new AbstractService() {
     @Override protected final void doStart() {
       executor().execute(new Runnable() {
         @Override
         public void run() {
           try {
             startUp();
             notifyStarted();
 
             if (isRunning()) {
               try {
                 AbstractExecutionThreadService.this.run();
               } catch (Throwable t) {
                 try {
                   shutDown();
                 } catch (Exception ignored) {
                   .log(.,
                       "Error while attempting to shut down the service"
                       + " after failure."ignored);
                 }
                 throw t;
               }
             }
 
             shutDown();
             notifyStopped();
           } catch (Throwable t) {
             notifyFailed(t);
             throw Throwables.propagate(t);
           }
         }
       });
     }
 
     @Override protected void doStop() {
       triggerShutdown();
     }
   };

  
Constructor for use by subclasses.
 
   protected AbstractExecutionThreadService() {}

  
Start the service. This method is invoked on the execution thread.

By default this method does nothing.

 
   protected void startUp() throws Exception {}

  
Run the service. This method is invoked on the execution thread. Implementations must respond to stop requests. You could poll for lifecycle changes in a work loop:
   public void run() {
     while (isRunning()) {
       // perform a unit of work
     }
   }
 
...or you could respond to stop requests by implementing triggerShutdown(), which should cause run() to return.
  protected abstract void run() throws Exception;

  
Stop the service. This method is invoked on the execution thread.

By default this method does nothing.

  // TODO: consider supporting a TearDownTestCase-like API
  protected void shutDown() throws Exception {}

  
Invoked to request the service to stop.

By default this method does nothing.

  protected void triggerShutdown() {}

  
Returns the java.util.concurrent.Executor that will be used to run this service. Subclasses may override this method to use a custom java.util.concurrent.Executor, which may configure its worker thread with a specific name, thread group or priority. The returned executor's execute() method is called when this service is started, and should return promptly.

The default implementation returns a new java.util.concurrent.Executor that sets the name of its threads to the string returned by getServiceName()

  protected Executor executor() {
    return new Executor() {
      @Override
      public void execute(Runnable command) {
        new Thread(commandgetServiceName()).start();
      }
    };
  }
  @Override public String toString() {
    return getServiceName() + " [" + state() + "]";
  }
  // We override instead of using ForwardingService so that these can be final.
  @Override public final ListenableFuture<Statestart() {
    return .start();
  }
  @Override public final State startAndWait() {
    return .startAndWait();
  }
  @Override public final boolean isRunning() {
    return .isRunning();
  }
  @Override public final State state() {
    return .state();
  }
  @Override public final ListenableFuture<Statestop() {
    return .stop();
  }
  @Override public final State stopAndWait() {
    return .stopAndWait();
  }
  @Override public final void addListener(Listener listenerExecutor executor) {
    .addListener(listenerexecutor);
  }

  
Returns the name of this service. AbstractExecutionThreadService may include the name in debugging output.

Subclasses may override this method.

Since:
10.0
  protected String getServiceName() {
    return getClass().getSimpleName();
  }
New to GrepCode? Check out our FAQ X