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 BooleanWhether this task has been canceled.static final intDefault number of tries to run this task.static final longDefault number of milliseconds to wait before a retry.static final longDefault timeout for this task.protected BooleanWhether this task is complete.protected intNumber of tries to execute this task.protected StringName for this task.protected intNumber of tries used.protected TThe future from the executor service.protected longNumber of milliseconds to wait before trying again.protected TimerTimer used to schedule retries, when they have a non-zero delay.protected ThreadThe thread where this is running, used to interrupt.protected ExecutorServiceExecutorService used to execute this task.protected final ObjectA synchronized objectprotected longTimeout 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 TypeMethodDescriptionbooleancancel(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.intMaximum number of tries before giving up.getName()intNumber of tries used.longbooleanbooleanisDone()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 triesvoidvoidsetRetryDelay(long retryDelay) voidsetRetryTimer(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:
isCancelledin interfaceFuture<T>
-
isDone
public boolean isDone() -
get
Get the result returned by the callable.- Specified by:
getin interfaceFuture<T>- Throws:
InterruptedExceptionExecutionException
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Get the result returned by the callable.- Specified by:
getin interfaceFuture<T>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
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
-