Package gov.usgs.util
Class ExecutorTask<T>
java.lang.Object
gov.usgs.util.ExecutorTask<T>
- Type Parameters:
T
- return type for callable.
- Direct Known Subclasses:
FutureExecutorTask
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
FieldsModifier and TypeFieldDescriptionThe callable to be called.protected Boolean
Whether this task has been canceled.static final int
Default number of tries to run this task.static final long
Default number of milliseconds to wait before a retry.static final long
Default timeout for this task.protected Boolean
Whether this task is complete.protected int
Number of tries to execute this task.protected String
Name for this task.protected int
Number of tries used.protected T
The future from the executor service.protected long
Number of milliseconds to wait before trying again.protected Timer
Timer used to schedule retries, when they have a non-zero delay.protected Thread
The thread where this is running, used to interrupt.protected ExecutorService
ExecutorService used to execute this task.protected final Object
A synchronized objectprotected long
Timeout for task. -
Constructor Summary
ConstructorsConstructorDescriptionExecutorTask
(ExecutorService service, int maxTries, long timeout, Runnable runnable, T result) Wraps a runnable and result using the CallableRunnable class.ExecutorTask
(ExecutorService service, int maxTries, long timeout, Callable<T> callable) Construct a new ExecutorTaskExecutorTask
(ExecutorService service, int maxTries, long timeout, Callable<T> callable, Timer retryTimer, long retryDelay) Construct a new ExecutorTask -
Method Summary
Modifier and TypeMethodDescriptionboolean
cancel
(boolean mayInterruptIfRunning) get()
Get the result returned by the callable.Get the result returned by the callable.The callable object that is/was called.Any exceptions thrown, during any execution attempt.int
Maximum number of tries before giving up.getName()
int
Number of tries used.long
boolean
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 triesvoid
void
setRetryDelay
(long retryDelay) void
setRetryTimer
(Timer retryTimer)
-
Field Details
-
DEFAULT_RETRY_DELAY
public static final long DEFAULT_RETRY_DELAYDefault number of milliseconds to wait before a retry.- See Also:
-
DEFAULT_NUM_TRIES
public static final int DEFAULT_NUM_TRIESDefault number of tries to run this task.- See Also:
-
DEFAULT_TIMEOUT
public static final long DEFAULT_TIMEOUTDefault timeout for this task.- See Also:
-
service
ExecutorService used to execute this task. -
callable
The callable to be called. -
timeout
protected long timeoutTimeout for task. -
maxTries
protected int maxTriesNumber of tries to execute this task. -
retryDelay
protected long retryDelayNumber of milliseconds to wait before trying again. -
retryTimer
Timer used to schedule retries, when they have a non-zero delay. -
result
The future from the executor service. -
done
Whether this task is complete. -
cancelled
Whether this task has been canceled. -
numTries
protected int numTriesNumber of tries used. -
runThread
The thread where this is running, used to interrupt. -
name
Name for this task. -
syncObject
A synchronized object
-
-
Constructor Details
-
ExecutorTask
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 runnableresult
- 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. -
setDone
protected void setDone()Called when task is completed, either successfully, or unsuccessfully and has no more tries -
cancel
public boolean cancel(boolean mayInterruptIfRunning) -
isCancelled
public boolean isCancelled()- Specified by:
isCancelled
in interfaceFuture<T>
-
isDone
public boolean isDone() -
get
Get the result returned by the callable.- Specified by:
get
in interfaceFuture<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 interfaceFuture<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
Any exceptions thrown, during any execution attempt.- Returns:
- array of thrown exceptions. should contain no more than numTries exceptions.
-
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
- Returns:
- name
-
setName
- Parameters:
name
- to set
-
getRetryDelay
public long getRetryDelay()- Returns:
- the retryDelay
-
setRetryDelay
public void setRetryDelay(long retryDelay) - Parameters:
retryDelay
- the retryDelay to set
-
getRetryTimer
- Returns:
- the retryTimer
-
setRetryTimer
- Parameters:
retryTimer
- the retryTimer to set
-