AbstractScheduledService.java 15 KB
package com.google.common.util.concurrent;

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
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.TimeoutException;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

@Beta
public abstract class AbstractScheduledService
  implements Service
{
  private static final Logger a = Logger.getLogger(AbstractScheduledService.class.getName());
  private final AbstractService b = new AbstractService()
  {
    private volatile Future<?> b;
    private volatile ScheduledExecutorService c;
    private final ReentrantLock d = new ReentrantLock();
    private final Runnable e = new Runnable()
    {
      /* Error */
      public final void run()
      {
        // Byte code:
        //   0: aload_0
        //   1: getfield 14	com/google/common/util/concurrent/AbstractScheduledService$1$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
        //   4: invokestatic 26	com/google/common/util/concurrent/AbstractScheduledService$1:a	(Lcom/google/common/util/concurrent/AbstractScheduledService$1;)Ljava/util/concurrent/locks/ReentrantLock;
        //   7: invokevirtual 31	java/util/concurrent/locks/ReentrantLock:lock	()V
        //   10: aload_0
        //   11: getfield 14	com/google/common/util/concurrent/AbstractScheduledService$1$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
        //   14: getfield 34	com/google/common/util/concurrent/AbstractScheduledService$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService;
        //   17: invokevirtual 39	com/google/common/util/concurrent/AbstractScheduledService:runOneIteration	()V
        //   20: aload_0
        //   21: getfield 14	com/google/common/util/concurrent/AbstractScheduledService$1$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
        //   24: invokestatic 26	com/google/common/util/concurrent/AbstractScheduledService$1:a	(Lcom/google/common/util/concurrent/AbstractScheduledService$1;)Ljava/util/concurrent/locks/ReentrantLock;
        //   27: invokevirtual 42	java/util/concurrent/locks/ReentrantLock:unlock	()V
        //   30: return
        //   31: astore_1
        //   32: aload_0
        //   33: getfield 14	com/google/common/util/concurrent/AbstractScheduledService$1$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
        //   36: getfield 34	com/google/common/util/concurrent/AbstractScheduledService$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService;
        //   39: invokevirtual 45	com/google/common/util/concurrent/AbstractScheduledService:shutDown	()V
        //   42: aload_0
        //   43: getfield 14	com/google/common/util/concurrent/AbstractScheduledService$1$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
        //   46: aload_1
        //   47: invokevirtual 49	com/google/common/util/concurrent/AbstractScheduledService$1:notifyFailed	(Ljava/lang/Throwable;)V
        //   50: aload_1
        //   51: invokestatic 55	com/google/common/base/Throwables:propagate	(Ljava/lang/Throwable;)Ljava/lang/RuntimeException;
        //   54: athrow
        //   55: astore_1
        //   56: aload_0
        //   57: getfield 14	com/google/common/util/concurrent/AbstractScheduledService$1$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
        //   60: invokestatic 26	com/google/common/util/concurrent/AbstractScheduledService$1:a	(Lcom/google/common/util/concurrent/AbstractScheduledService$1;)Ljava/util/concurrent/locks/ReentrantLock;
        //   63: invokevirtual 42	java/util/concurrent/locks/ReentrantLock:unlock	()V
        //   66: aload_1
        //   67: athrow
        //   68: astore_2
        //   69: invokestatic 58	com/google/common/util/concurrent/AbstractScheduledService:a	()Ljava/util/logging/Logger;
        //   72: getstatic 64	java/util/logging/Level:WARNING	Ljava/util/logging/Level;
        //   75: ldc 66
        //   77: aload_2
        //   78: invokevirtual 72	java/util/logging/Logger:log	(Ljava/util/logging/Level;Ljava/lang/String;Ljava/lang/Throwable;)V
        //   81: goto -39 -> 42
        // Local variable table:
        //   start	length	slot	name	signature
        //   0	84	0	this	1
        //   31	20	1	localThrowable	Throwable
        //   55	12	1	localObject	Object
        //   68	10	2	localException	Exception
        // Exception table:
        //   from	to	target	type
        //   10	20	31	java/lang/Throwable
        //   10	20	55	finally
        //   32	42	55	finally
        //   42	55	55	finally
        //   69	81	55	finally
        //   32	42	68	java/lang/Exception
      }
    };
    
    protected final void doStart()
    {
      this.c = MoreExecutors.a(AbstractScheduledService.this.executor(), new Supplier() {});
      this.c.execute(new Runnable()
      {
        public final void run()
        {
          AbstractScheduledService.1.a(AbstractScheduledService.1.this).lock();
          try
          {
            AbstractScheduledService.this.startUp();
            AbstractScheduledService.1 local1 = AbstractScheduledService.1.this;
            AbstractScheduledService.Scheduler localScheduler = AbstractScheduledService.this.scheduler();
            AbstractScheduledService.a(AbstractScheduledService.this);
            AbstractScheduledService.1.a(local1, localScheduler.a(AbstractScheduledService.1.b(AbstractScheduledService.1.this), AbstractScheduledService.1.c(AbstractScheduledService.1.this)));
            AbstractScheduledService.1.this.notifyStarted();
            return;
          }
          catch (Throwable localThrowable)
          {
            AbstractScheduledService.1.this.notifyFailed(localThrowable);
            throw Throwables.propagate(localThrowable);
          }
          finally
          {
            AbstractScheduledService.1.a(AbstractScheduledService.1.this).unlock();
          }
        }
      });
    }
    
    protected final void doStop()
    {
      this.b.cancel(false);
      this.c.execute(new Runnable()
      {
        /* Error */
        public final void run()
        {
          // Byte code:
          //   0: aload_0
          //   1: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   4: invokestatic 26	com/google/common/util/concurrent/AbstractScheduledService$1:a	(Lcom/google/common/util/concurrent/AbstractScheduledService$1;)Ljava/util/concurrent/locks/ReentrantLock;
          //   7: invokevirtual 31	java/util/concurrent/locks/ReentrantLock:lock	()V
          //   10: aload_0
          //   11: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   14: invokevirtual 35	com/google/common/util/concurrent/AbstractScheduledService$1:state	()Lcom/google/common/util/concurrent/Service$State;
          //   17: astore_1
          //   18: getstatic 41	com/google/common/util/concurrent/Service$State:STOPPING	Lcom/google/common/util/concurrent/Service$State;
          //   21: astore_2
          //   22: aload_1
          //   23: aload_2
          //   24: if_acmpeq +14 -> 38
          //   27: aload_0
          //   28: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   31: invokestatic 26	com/google/common/util/concurrent/AbstractScheduledService$1:a	(Lcom/google/common/util/concurrent/AbstractScheduledService$1;)Ljava/util/concurrent/locks/ReentrantLock;
          //   34: invokevirtual 44	java/util/concurrent/locks/ReentrantLock:unlock	()V
          //   37: return
          //   38: aload_0
          //   39: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   42: getfield 47	com/google/common/util/concurrent/AbstractScheduledService$1:a	Lcom/google/common/util/concurrent/AbstractScheduledService;
          //   45: invokevirtual 52	com/google/common/util/concurrent/AbstractScheduledService:shutDown	()V
          //   48: aload_0
          //   49: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   52: invokestatic 26	com/google/common/util/concurrent/AbstractScheduledService$1:a	(Lcom/google/common/util/concurrent/AbstractScheduledService$1;)Ljava/util/concurrent/locks/ReentrantLock;
          //   55: invokevirtual 44	java/util/concurrent/locks/ReentrantLock:unlock	()V
          //   58: aload_0
          //   59: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   62: invokevirtual 55	com/google/common/util/concurrent/AbstractScheduledService$1:notifyStopped	()V
          //   65: return
          //   66: astore_1
          //   67: aload_0
          //   68: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   71: aload_1
          //   72: invokevirtual 59	com/google/common/util/concurrent/AbstractScheduledService$1:notifyFailed	(Ljava/lang/Throwable;)V
          //   75: aload_1
          //   76: invokestatic 65	com/google/common/base/Throwables:propagate	(Ljava/lang/Throwable;)Ljava/lang/RuntimeException;
          //   79: athrow
          //   80: astore_1
          //   81: aload_0
          //   82: getfield 17	com/google/common/util/concurrent/AbstractScheduledService$1$4:a	Lcom/google/common/util/concurrent/AbstractScheduledService$1;
          //   85: invokestatic 26	com/google/common/util/concurrent/AbstractScheduledService$1:a	(Lcom/google/common/util/concurrent/AbstractScheduledService$1;)Ljava/util/concurrent/locks/ReentrantLock;
          //   88: invokevirtual 44	java/util/concurrent/locks/ReentrantLock:unlock	()V
          //   91: aload_1
          //   92: athrow
          // Local variable table:
          //   start	length	slot	name	signature
          //   0	93	0	this	4
          //   17	6	1	localState1	Service.State
          //   66	10	1	localThrowable	Throwable
          //   80	12	1	localObject	Object
          //   21	3	2	localState2	Service.State
          // Exception table:
          //   from	to	target	type
          //   0	10	66	java/lang/Throwable
          //   27	37	66	java/lang/Throwable
          //   48	65	66	java/lang/Throwable
          //   81	93	66	java/lang/Throwable
          //   10	22	80	finally
          //   38	48	80	finally
        }
      });
    }
  };
  
  public final void addListener(Service.Listener paramListener, Executor paramExecutor)
  {
    this.b.addListener(paramListener, paramExecutor);
  }
  
  public final void awaitRunning()
  {
    this.b.awaitRunning();
  }
  
  public final void awaitRunning(long paramLong, TimeUnit paramTimeUnit)
    throws TimeoutException
  {
    this.b.awaitRunning(paramLong, paramTimeUnit);
  }
  
  public final void awaitTerminated()
  {
    this.b.awaitTerminated();
  }
  
  public final void awaitTerminated(long paramLong, TimeUnit paramTimeUnit)
    throws TimeoutException
  {
    this.b.awaitTerminated(paramLong, paramTimeUnit);
  }
  
  protected ScheduledExecutorService executor()
  {
    final ScheduledExecutorService localScheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory()
    {
      public final Thread newThread(Runnable paramAnonymousRunnable)
      {
        return MoreExecutors.a(AbstractScheduledService.this.serviceName(), paramAnonymousRunnable);
      }
    });
    addListener(new Service.Listener()
    {
      public final void failed(Service.State paramAnonymousState, Throwable paramAnonymousThrowable)
      {
        localScheduledExecutorService.shutdown();
      }
      
      public final void terminated(Service.State paramAnonymousState)
      {
        localScheduledExecutorService.shutdown();
      }
    }, MoreExecutors.directExecutor());
    return localScheduledExecutorService;
  }
  
  public final Throwable failureCause()
  {
    return this.b.failureCause();
  }
  
  public final boolean isRunning()
  {
    return this.b.isRunning();
  }
  
  protected abstract void runOneIteration()
    throws Exception;
  
  protected abstract Scheduler scheduler();
  
  protected String serviceName()
  {
    return getClass().getSimpleName();
  }
  
  protected void shutDown()
    throws Exception
  {}
  
  public final Service startAsync()
  {
    this.b.startAsync();
    return this;
  }
  
  protected void startUp()
    throws Exception
  {}
  
  public final Service.State state()
  {
    return this.b.state();
  }
  
  public final Service stopAsync()
  {
    this.b.stopAsync();
    return this;
  }
  
  public String toString()
  {
    String str1 = String.valueOf(String.valueOf(serviceName()));
    String str2 = String.valueOf(String.valueOf(state()));
    return str1.length() + 3 + str2.length() + str1 + " [" + str2 + "]";
  }
  
  @Beta
  public static abstract class CustomScheduler
    extends AbstractScheduledService.Scheduler
  {
    public CustomScheduler()
    {
      super();
    }
    
    protected abstract Schedule getNextSchedule()
      throws Exception;
    
    @Beta
    public static final class Schedule
    {
      private final long a;
      private final TimeUnit b;
      
      public Schedule(long paramLong, TimeUnit paramTimeUnit)
      {
        this.a = paramLong;
        this.b = ((TimeUnit)Preconditions.checkNotNull(paramTimeUnit));
      }
    }
  }
  
  public static abstract class Scheduler
  {
    public static Scheduler newFixedDelaySchedule(long paramLong1, long paramLong2, final TimeUnit paramTimeUnit)
    {
      new Scheduler(paramLong1)
      {
        public final Future<?> a(ScheduledExecutorService paramAnonymousScheduledExecutorService, Runnable paramAnonymousRunnable)
        {
          return paramAnonymousScheduledExecutorService.scheduleWithFixedDelay(paramAnonymousRunnable, this.a, paramTimeUnit, this.c);
        }
      };
    }
    
    public static Scheduler newFixedRateSchedule(long paramLong1, long paramLong2, final TimeUnit paramTimeUnit)
    {
      new Scheduler(paramLong1)
      {
        public final Future<?> a(ScheduledExecutorService paramAnonymousScheduledExecutorService, Runnable paramAnonymousRunnable)
        {
          return paramAnonymousScheduledExecutorService.scheduleAtFixedRate(paramAnonymousRunnable, this.a, paramTimeUnit, this.c);
        }
      };
    }
    
    abstract Future<?> a(ScheduledExecutorService paramScheduledExecutorService, Runnable paramRunnable);
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/google/common/util/concurrent/AbstractScheduledService.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */