Package gov.usgs.util

Class ExecutorTask<T>

  • Type Parameters:
    T - return type for callable.
    All Implemented Interfaces:
    java.lang.Runnable, java.util.concurrent.Future<T>
    Direct Known Subclasses:
    FutureExecutorTask

    public class ExecutorTask<T>
    extends java.lang.Object
    implements java.util.concurrent.Future<T>, java.lang.Runnable
    A wrapper for Runnable or Callable objects for use with an ExecutorService. Can be used to schedule interrupt based timeouts, multiple attempts, and Future style exception tracking for Runnable or Callable objects.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.Callable<T> callable
      The callable to be called.
      protected java.lang.Boolean cancelled
      Whether this task has been canceled.
      static int DEFAULT_NUM_TRIES
      Default number of tries to run this task.
      static long DEFAULT_RETRY_DELAY
      Default number of milliseconds to wait before a retry.
      static long DEFAULT_TIMEOUT
      Default timeout for this task.
      protected java.lang.Boolean done
      Whether this task is complete.
      protected int maxTries
      Number of tries to execute this task.
      protected java.lang.String name
      Name for this task.
      protected int numTries
      Number of tries used.
      protected T result
      The future from the executor service.
      protected long retryDelay
      Number of milliseconds to wait before trying again.
      protected java.util.Timer retryTimer
      Timer used to schedule retries, when they have a non-zero delay.
      protected java.lang.Thread runThread
      The thread where this is running, used to interrupt.
      protected java.util.concurrent.ExecutorService service
      ExecutorService used to execute this task.
      protected java.lang.Object syncObject
      A synchronized object
      protected long timeout
      Timeout for task.
    • Constructor Summary

      Constructors 
      Constructor Description
      ExecutorTask​(java.util.concurrent.ExecutorService service, int maxTries, long timeout, java.lang.Runnable runnable, T result)
      Wraps a runnable and result using the CallableRunnable class.
      ExecutorTask​(java.util.concurrent.ExecutorService service, int maxTries, long timeout, java.util.concurrent.Callable<T> callable)
      Construct a new ExecutorTask
      ExecutorTask​(java.util.concurrent.ExecutorService service, int maxTries, long timeout, java.util.concurrent.Callable<T> callable, java.util.Timer retryTimer, long retryDelay)
      Construct a new ExecutorTask
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean cancel​(boolean mayInterruptIfRunning)  
      T get()
      Get the result returned by the callable.
      T get​(long timeout, java.util.concurrent.TimeUnit unit)
      Get the result returned by the callable.
      java.util.concurrent.Callable<T> getCallable()
      The callable object that is/was called.
      java.util.ArrayList<java.lang.Exception> getExceptions()
      Any exceptions thrown, during any execution attempt.
      int getMaxTries()
      Maximum number of tries before giving up.
      java.lang.String getName()  
      int getNumTries()
      Number of tries used.
      long getRetryDelay()  
      java.util.Timer getRetryTimer()  
      boolean isCancelled()  
      boolean isDone()  
      void run()
      Run calls the callable, scheduling timeout interruption, catching exceptions, and potentially resubmitting to the executor service.
      protected void setDone()
      Called when task is completed, either successfully, or unsuccessfully and has no more tries
      void setName​(java.lang.String name)  
      void setRetryDelay​(long retryDelay)  
      void setRetryTimer​(java.util.Timer retryTimer)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_RETRY_DELAY

        public static final long DEFAULT_RETRY_DELAY
        Default number of milliseconds to wait before a retry.
        See Also:
        Constant Field Values
      • DEFAULT_NUM_TRIES

        public static final int DEFAULT_NUM_TRIES
        Default number of tries to run this task.
        See Also:
        Constant Field Values
      • DEFAULT_TIMEOUT

        public static final long DEFAULT_TIMEOUT
        Default timeout for this task.
        See Also:
        Constant Field Values
      • service

        protected java.util.concurrent.ExecutorService service
        ExecutorService used to execute this task.
      • callable

        protected java.util.concurrent.Callable<T> callable
        The callable to be called.
      • timeout

        protected long timeout
        Timeout for task.
      • maxTries

        protected int maxTries
        Number of tries to execute this task.
      • retryDelay

        protected long retryDelay
        Number of milliseconds to wait before trying again.
      • retryTimer

        protected java.util.Timer retryTimer
        Timer used to schedule retries, when they have a non-zero delay.
      • result

        protected T result
        The future from the executor service.
      • done

        protected java.lang.Boolean done
        Whether this task is complete.
      • cancelled

        protected java.lang.Boolean cancelled
        Whether this task has been canceled.
      • numTries

        protected int numTries
        Number of tries used.
      • runThread

        protected java.lang.Thread runThread
        The thread where this is running, used to interrupt.
      • name

        protected java.lang.String name
        Name for this task.
      • syncObject

        protected final java.lang.Object syncObject
        A synchronized object
    • Constructor Detail

      • ExecutorTask

        public ExecutorTask​(java.util.concurrent.ExecutorService service,
                            int maxTries,
                            long timeout,
                            java.util.concurrent.Callable<T> callable)
        Construct a new ExecutorTask
        Parameters:
        service - ExecutorService that this task will be submitted to.
        maxTries - maximum number of tries callable can throw an exception or timeout before giving up. < 1 means never run.
        timeout - number of milliseconds to allow callable to run before it is interrupted. <= 0 means never timeout.
        callable - the callable to call. To work well, the callable should handle interrupts gracefully.
        See Also:
        InterruptedException
      • ExecutorTask

        public ExecutorTask​(java.util.concurrent.ExecutorService service,
                            int maxTries,
                            long timeout,
                            java.lang.Runnable runnable,
                            T result)
        Wraps a runnable and result using the CallableRunnable class.
        Parameters:
        service - ExecutorService that this task will be submitted to.
        maxTries - maximum number of tries callable can throw an exception or timeout before giving up. < 1 means never run.
        timeout - number of milliseconds to allow callable to run before it is interrupted. <= 0 means never timeout.
        runnable - a runnable
        result - the result
        See Also:
        Executors.callable(Runnable, Object)
      • ExecutorTask

        public ExecutorTask​(java.util.concurrent.ExecutorService service,
                            int maxTries,
                            long timeout,
                            java.util.concurrent.Callable<T> callable,
                            java.util.Timer retryTimer,
                            long retryDelay)
        Construct a new ExecutorTask
        Parameters:
        service - ExecutorService that this task will be submitted to.
        maxTries - maximum number of tries callable can throw an exception or timeout before giving up. < 1 means never run.
        timeout - number of milliseconds to allow callable to run before it is interrupted. <= 0 means never timeout.
        callable - the callable to call. To work well, the callable should handle interrupts gracefully.
        retryTimer - a timer used to schedule retries when retryDelay is non-zero.
        retryDelay - the number of milliseconds to wait before retrying after an exception.
        See Also:
        InterruptedException
    • Method Detail

      • run

        public void run()
        Run calls the callable, scheduling timeout interruption, catching exceptions, and potentially resubmitting to the executor service.
        Specified by:
        run in interface java.lang.Runnable
      • setDone

        protected void setDone()
        Called when task is completed, either successfully, or unsuccessfully and has no more tries
      • cancel

        public boolean cancel​(boolean mayInterruptIfRunning)
        Specified by:
        cancel in interface java.util.concurrent.Future<T>
      • isCancelled

        public boolean isCancelled()
        Specified by:
        isCancelled in interface java.util.concurrent.Future<T>
      • isDone

        public boolean isDone()
        Specified by:
        isDone in interface java.util.concurrent.Future<T>
      • get

        public T get()
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException
        Get the result returned by the callable.
        Specified by:
        get in interface java.util.concurrent.Future<T>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • get

        public T get​(long timeout,
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException,
                     java.util.concurrent.TimeoutException
        Get the result returned by the callable.
        Specified by:
        get in interface java.util.concurrent.Future<T>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • getNumTries

        public int getNumTries()
        Number of tries used.
        Returns:
        actual number of attempts.
      • getMaxTries

        public int getMaxTries()
        Maximum number of tries before giving up.
        Returns:
        maximum number of attempts.
      • getExceptions

        public java.util.ArrayList<java.lang.Exception> getExceptions()
        Any exceptions thrown, during any execution attempt.
        Returns:
        array of thrown exceptions. should contain no more than numTries exceptions.
      • getCallable

        public java.util.concurrent.Callable<T> getCallable()
        The callable object that is/was called.
        Returns:
        The callable object for this task. If this task was created using a runnable, this was created using Executors.callable(Runnable).
      • getName

        public java.lang.String getName()
        Returns:
        name
      • setName

        public void setName​(java.lang.String name)
        Parameters:
        name - to set
      • getRetryDelay

        public long getRetryDelay()
        Returns:
        the retryDelay
      • setRetryDelay

        public void setRetryDelay​(long retryDelay)
        Parameters:
        retryDelay - the retryDelay to set
      • getRetryTimer

        public java.util.Timer getRetryTimer()
        Returns:
        the retryTimer
      • setRetryTimer

        public void setRetryTimer​(java.util.Timer retryTimer)
        Parameters:
        retryTimer - the retryTimer to set