Package org.lsst.ccs.services
Class AgentExecutionService.Task<T>
java.lang.Object
org.lsst.ccs.services.AgentExecutionService.Task<T>
- Type Parameters:
T- Type of the result produced by this task.
- All Implemented Interfaces:
Future<T>
- Enclosing class:
- AgentExecutionService
Represents a customizable task to be executed by
AgentExecutionService.
Tasks are created by calling one of the AgentExecutionService.task(...) methods.
Tasks can be customized before execution by specifying thread name, exception handler,
log level, and restart policy. All setters should be called before the task is submitted
for execution by calling its start() method. For example:
AgentExecutionService service = agent.getAgentService(AgentExecutionService.class);
service.task(runner1)
.setName("background task")
.setRestart(2, 1, TimeUnit.DAYS)
.setLogLevel(Level.WARNING)
.setExceptionHandler((task, throwable, willRestart) -> {
String message = "Task "+ task.getName();
if (willRestart) {
message += " threw an exception, will restart.";
} else {
message += " failed.";
}
task.getLogger().log(task.getLogLevel(), message, throwable);
return true;
})
.start();
Instance of Task can be used to monitor and control running tasks, and to
retrieve their results.-
Method Summary
Modifier and TypeMethodDescriptionbooleancancel(boolean mayInterruptIfRunning) Attempts to cancel execution of this task.get()Waits if necessary for the computation to complete, and then retrieves its result.Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.Returns the logger for this task.Returns the log level for this task.getName()Returns the name of this task.booleanChecks whether this task was canceled before it completed normally.booleanisDone()Returnstrueif this task completed.setExceptionHandler(AgentExecutionService.ExceptionHandler exceptionHandler) Sets the exception handler for this task.Sets the logger for this task.setLogLevel(Level level) Sets the logging level for this task.Sets the name of this task.setRestart(int maxAttempts, long time, TimeUnit unit) Sets the restart policy for this task.start()Starts the task execution.
-
Method Details
-
getName
Returns the name of this task.- Returns:
- Task name.
-
getLogger
Returns the logger for this task.- Returns:
- Logger.
-
getLogLevel
Returns the log level for this task.- Returns:
- Log level.
-
setName
Sets the name of this task. The name will be used as a thread name while this task is executing.- Parameters:
name- Task name.- Returns:
- This task.
- Throws:
IllegalStateException- if the task has already been started.
-
setExceptionHandler
public AgentExecutionService.Task<T> setExceptionHandler(AgentExecutionService.ExceptionHandler exceptionHandler) Sets the exception handler for this task. If this method is never called, the default handler is used. The default handler logs a message at INFO level.- Parameters:
exceptionHandler- Exception handler to be used by this task.- Returns:
- This task.
- Throws:
IllegalStateException- if the task has already been started.
-
setLogger
Sets the logger for this task.- Parameters:
logger- Logger.- Returns:
- This task.
- Throws:
IllegalStateException- if the task has already been started.
-
setLogLevel
Sets the logging level for this task.- Parameters:
level- Level.- Returns:
- This task.
- Throws:
IllegalStateException- if the task has already been started.
-
setRestart
Sets the restart policy for this task.- Parameters:
maxAttempts- Maximum number of times per specified time period this task will be restarted when it throws an uncaught exception. If zero, this task will never restart. If negative, the task will always restart unless its exception handler returnsfalse. The value of this parameter should not exceed 100.time- Length of time period for counting restarts. If zero, restarts are counted towardsmaxAttemptsthroughout the lifetime of the task.unit- Unit for time period.- Returns:
- This task.
- Throws:
IllegalStateException- if the task has already been started.IllegalArgumentException- ifmaxAttemptsis above 100 ortimeis negative.
-
start
Starts the task execution.- Returns:
- This task.
- Throws:
RejectedExecutionException- if the task cannot be accepted for execution
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been canceled, or could not be canceled for some other reason. If successful, and this task has not started whencancelis called, this task should never run. If the task has already started, then themayInterruptIfRunningparameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.After this method returns, subsequent calls to
isDone()will always returntrue. Subsequent calls toisCancelled()will always returntrueif this method returnedtrue.- Specified by:
cancelin interfaceFuture<T>- Parameters:
mayInterruptIfRunning-trueif the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.- Returns:
falseif the task could not be canceled, typically because it has already completed normally;trueotherwise.
-
isCancelled
public boolean isCancelled()Checks whether this task was canceled before it completed normally.- Specified by:
isCancelledin interfaceFuture<T>- Returns:
trueif this task was canceled.
-
isDone
public boolean isDone()Returnstrueif this task completed. Completion may be due to normal termination, an exception, or cancellation - in all of these cases, this method will returntrue. -
get
Waits if necessary for the computation to complete, and then retrieves its result.- Specified by:
getin interfaceFuture<T>- Returns:
- Computed result.
- Throws:
CancellationException- if the computation was canceled.ExecutionException- if the computation threw an exception.InterruptedException- if the current thread was interrupted while waiting.
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.- Specified by:
getin interfaceFuture<T>- Parameters:
timeout- Maximum time to wait.unit- Time unit of the timeout argument.- Returns:
- Computed result.
- Throws:
CancellationException- if the computation was canceled.ExecutionException- if the computation threw an exception.InterruptedException- if the current thread was interrupted while waiting.TimeoutException- if the wait timed out.
-