diff options
Diffstat (limited to 'guava/src/com/google/common/util/concurrent/AbstractScheduledService.java')
-rw-r--r-- | guava/src/com/google/common/util/concurrent/AbstractScheduledService.java | 89 |
1 files changed, 13 insertions, 76 deletions
diff --git a/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java b/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java index 949f76a..f847205 100644 --- a/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java +++ b/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java @@ -21,11 +21,9 @@ import com.google.common.base.Preconditions; import com.google.common.base.Throwables; import java.util.concurrent.Callable; -import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; @@ -98,8 +96,9 @@ public abstract class AbstractScheduledService implements Service { * * <p>Consider using the {@link #newFixedDelaySchedule} and {@link #newFixedRateSchedule} factory * methods, these provide {@link Scheduler} instances for the common use case of running the - * service with a fixed schedule. If more flexibility is needed then consider subclassing - * {@link CustomScheduler}. + * service with a fixed schedule. If more flexibility is needed then consider subclassing the + * {@link CustomScheduler} abstract class in preference to creating your own {@link Scheduler} + * implementation. * * @author Luke Sandberg * @since 11.0 @@ -230,9 +229,6 @@ public abstract class AbstractScheduledService implements Service { } }; - /** Constructor for use by subclasses. */ - protected AbstractScheduledService() {} - /** * Run one iteration of the scheduled task. If any invocation of this method throws an exception, * the service will transition to the {@link Service.State#FAILED} state and this method will no @@ -240,19 +236,11 @@ public abstract class AbstractScheduledService implements Service { */ protected abstract void runOneIteration() throws Exception; - /** - * Start the service. - * - * <p>By default this method does nothing. - */ - protected void startUp() throws Exception {} + /** Start the service. */ + protected abstract void startUp() throws Exception; - /** - * Stop the service. This is guaranteed not to run concurrently with {@link #runOneIteration}. - * - * <p>By default this method does nothing. - */ - protected void shutDown() throws Exception {} + /** Stop the service. This is guaranteed not to run concurrently with {@link #runOneIteration}. */ + protected abstract void shutDown() throws Exception; /** * Returns the {@link Scheduler} object used to configure this service. This method will only be @@ -262,56 +250,19 @@ public abstract class AbstractScheduledService implements Service { /** * Returns the {@link ScheduledExecutorService} that will be used to execute the {@link #startUp}, - * {@link #runOneIteration} and {@link #shutDown} methods. If this method is overridden the - * executor will not be {@linkplain ScheduledExecutorService#shutdown shutdown} when this - * service {@linkplain Service.State#TERMINATED terminates} or - * {@linkplain Service.State#TERMINATED fails}. Subclasses may override this method to supply a - * custom {@link ScheduledExecutorService} instance. This method is guaranteed to only be called - * once. + * {@link #runOneIteration} and {@link #shutDown} methods. The executor will not be + * {@link ScheduledExecutorService#shutdown} when this service stops. Subclasses may override this + * method to use a custom {@link ScheduledExecutorService} instance. * * <p>By default this returns a new {@link ScheduledExecutorService} with a single thread thread - * pool that sets the name of the thread to the {@linkplain #serviceName() service name}. - * Also, the pool will be {@linkplain ScheduledExecutorService#shutdown() shut down} when the - * service {@linkplain Service.State#TERMINATED terminates} or - * {@linkplain Service.State#TERMINATED fails}. + * pool. This method will only be called once. */ protected ScheduledExecutorService executor() { - final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( - new ThreadFactory() { - @Override public Thread newThread(Runnable runnable) { - return MoreExecutors.newThread(serviceName(), runnable); - } - }); - // Add a listener to shutdown the executor after the service is stopped. This ensures that the - // JVM shutdown will not be prevented from exiting after this service has stopped or failed. - // Technically this listener is added after start() was called so it is a little gross, but it - // is called within doStart() so we know that the service cannot terminate or fail concurrently - // with adding this listener so it is impossible to miss an event that we are interested in. - addListener(new Listener() { - @Override public void starting() {} - @Override public void running() {} - @Override public void stopping(State from) {} - @Override public void terminated(State from) { - executor.shutdown(); - } - @Override public void failed(State from, Throwable failure) { - executor.shutdown(); - }}, MoreExecutors.sameThreadExecutor()); - return executor; + return Executors.newSingleThreadScheduledExecutor(); } - /** - * Returns the name of this service. {@link AbstractScheduledService} may include the name in - * debugging output. - * - * @since 14.0 - */ - protected String serviceName() { - return getClass().getSimpleName(); - } - @Override public String toString() { - return serviceName() + " [" + state() + "]"; + return getClass().getSimpleName() + " [" + state() + "]"; } // We override instead of using ForwardingService so that these can be final. @@ -341,20 +292,6 @@ public abstract class AbstractScheduledService implements Service { } /** - * @since 13.0 - */ - @Override public final void addListener(Listener listener, Executor executor) { - delegate.addListener(listener, executor); - } - - /** - * @since 14.0 - */ - @Override public final Throwable failureCause() { - return delegate.failureCause(); - } - - /** * A {@link Scheduler} that provides a convenient way for the {@link AbstractScheduledService} to * use a dynamically changing schedule. After every execution of the task, assuming it hasn't * been cancelled, the {@link #getNextSchedule} method will be called. |