Package gov.usgs.util

Class ExecutorTask<T>

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

public class ExecutorTask<T> extends Object implements Future<T>, 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 Details

    • DEFAULT_RETRY_DELAY

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

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

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

      protected ExecutorService service
      ExecutorService used to execute this task.
    • callable

      protected 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 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 Boolean done
      Whether this task is complete.
    • cancelled

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

      protected int numTries
      Number of tries used.
    • runThread

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

      protected String name
      Name for this task.
    • syncObject

      protected final Object syncObject
      A synchronized object
  • Constructor Details

    • ExecutorTask

      public ExecutorTask(ExecutorService service, int maxTries, long timeout, 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:
    • ExecutorTask

      public ExecutorTask(ExecutorService service, int maxTries, long timeout, 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:
    • ExecutorTask

      public ExecutorTask(ExecutorService service, int maxTries, long timeout, Callable<T> callable, 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:
  • Method Details

    • 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 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 Future<T>
    • isCancelled

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

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

      Get the result returned by the callable.
      Specified by:
      get in interface Future<T>
      Throws:
      InterruptedException
      ExecutionException
    • get

      public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Get the result returned by the callable.
      Specified by:
      get in interface Future<T>
      Throws:
      InterruptedException
      ExecutionException
      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 ArrayList<Exception> getExceptions()
      Any exceptions thrown, during any execution attempt.
      Returns:
      array of thrown exceptions. should contain no more than numTries exceptions.
    • getCallable

      public 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 String getName()
      Returns:
      name
    • setName

      public void setName(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 Timer getRetryTimer()
      Returns:
      the retryTimer
    • setRetryTimer

      public void setRetryTimer(Timer retryTimer)
      Parameters:
      retryTimer - the retryTimer to set