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 do not need a thread while "running" but may need one during startup and shutdown. Subclasses can implement startUp() and shutDown() methods, each which run in a executor which by default uses a separate thread for each method.

Author(s):
Chris Nokleberg
Since:
1.0
 
 public abstract class AbstractIdleService implements Service {
 
   /* Thread names will look like {@code "MyService STARTING"}. */
   private final Supplier<StringthreadNameSupplier = new Supplier<String>() {
     @Override public String get() {
       return serviceName() + " " + state();
     }
   };
 
   /* use AbstractService for state management */
   private final Service delegate = new AbstractService() {
     @Override protected final void doStart() {
       MoreExecutors.renamingDecorator(executor(), )
           .execute(new Runnable() {
             @Override public void run() {
               try {
                 startUp();
                 notifyStarted();
               } catch (Throwable t) {
                 notifyFailed(t);
                 throw Throwables.propagate(t);
               }
             }
           });
     }
 
     @Override protected final void doStop() {
       MoreExecutors.renamingDecorator(executor(), )
           .execute(new Runnable() {
             @Override public void run() {
               try {
                 shutDown();
                 notifyStopped();
               } catch (Throwable t) {
                 notifyFailed(t);
                 throw Throwables.propagate(t);
               }
             }
           });
     }
   };

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

  
Start the service.
 
   protected abstract void startUp() throws Exception;

  
Stop the service.
 
   protected abstract void shutDown() throws Exception;

  
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 stopped, and should return promptly.
 
   protected Executor executor() {
     return new Executor() {
       @Override public void execute(Runnable command) {
        MoreExecutors.newThread(.get(), command).start();
      }
    };
  }
  @Override public String toString() {
    return serviceName() + " [" + state() + "]";
  }
  // We override instead of using ForwardingService so that these can be final.
   public final ListenableFuture<Statestart() {
    return .start();
  }
   public final State startAndWait() {
    return .startAndWait();
  }
  @Override public final boolean isRunning() {
    return .isRunning();
  }
  @Override public final State state() {
    return .state();
  }
  public final ListenableFuture<Statestop() {
    return .stop();
  }
  public final State stopAndWait() {
    return .stopAndWait();
  }
  
  

Since:
13.0
  @Override public final void addListener(Listener listenerExecutor executor) {
    .addListener(listenerexecutor);
  }
  
  

Since:
14.0
  @Override public final Throwable failureCause() {
    return .failureCause();
  }
  
  

Since:
15.0
  @Override public final Service startAsync() {
    return this;
  }
  
  

Since:
15.0
  @Override public final Service stopAsync() {
    return this;
  }
  
  

Since:
15.0
  @Override public final void awaitRunning() {
  }
  
  

Since:
15.0
  @Override public final void awaitRunning(long timeoutTimeUnit unitthrows TimeoutException {
    .awaitRunning(timeoutunit);
  }
  
  

Since:
15.0
  @Override public final void awaitTerminated() {
  }
  
  

Since:
15.0
  @Override public final void awaitTerminated(long timeoutTimeUnit unitthrows TimeoutException {
    .awaitTerminated(timeoutunit);
  }
  
  
Returns the name of this service. AbstractIdleService may include the name in debugging output.

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