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:
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.RunnableA 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>callableThe callable to be called.protected java.lang.BooleancancelledWhether this task has been canceled.static intDEFAULT_NUM_TRIESDefault number of tries to run this task.static longDEFAULT_RETRY_DELAYDefault number of milliseconds to wait before a retry.static longDEFAULT_TIMEOUTDefault timeout for this task.protected java.lang.BooleandoneWhether this task is complete.protected intmaxTriesNumber of tries to execute this task.protected java.lang.StringnameName for this task.protected intnumTriesNumber of tries used.protected TresultThe future from the executor service.protected longretryDelayNumber of milliseconds to wait before trying again.protected java.util.TimerretryTimerTimer used to schedule retries, when they have a non-zero delay.protected java.lang.ThreadrunThreadThe thread where this is running, used to interrupt.protected java.util.concurrent.ExecutorServiceserviceExecutorService used to execute this task.protected java.lang.ObjectsyncObjectA synchronized objectprotected longtimeoutTimeout 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 ExecutorTaskExecutorTask(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 booleancancel(boolean mayInterruptIfRunning)Tget()Get the result returned by the callable.Tget(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.intgetMaxTries()Maximum number of tries before giving up.java.lang.StringgetName()intgetNumTries()Number of tries used.longgetRetryDelay()java.util.TimergetRetryTimer()booleanisCancelled()booleanisDone()voidrun()Run calls the callable, scheduling timeout interruption, catching exceptions, and potentially resubmitting to the executor service.protected voidsetDone()Called when task is completed, either successfully, or unsuccessfully and has no more triesvoidsetName(java.lang.String name)voidsetRetryDelay(long retryDelay)voidsetRetryTimer(java.util.Timer retryTimer)
-
-
-
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 runnableresult- 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:
runin interfacejava.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:
cancelin interfacejava.util.concurrent.Future<T>
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfacejava.util.concurrent.Future<T>
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.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:
getin interfacejava.util.concurrent.Future<T>- Throws:
java.lang.InterruptedExceptionjava.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:
getin interfacejava.util.concurrent.Future<T>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.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
-
-