
T - Type of the result produced by this task.public class AgentExecutionService.Task<T> extends Object implements Future<T>
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.| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task.
|
T |
get()
Waits if necessary for the computation to complete, and then retrieves its result.
|
T |
get(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for the computation to
complete, and then retrieves its result, if available.
|
Logger |
getLogger()
Returns the logger for this task.
|
Level |
getLogLevel()
Returns the log level for this task.
|
String |
getName()
Returns the name of this task.
|
boolean |
isCancelled()
Checks whether this task was canceled before it completed normally.
|
boolean |
isDone()
Returns
true if this task completed. |
AgentExecutionService.Task<T> |
setExceptionHandler(AgentExecutionService.ExceptionHandler exceptionHandler)
Sets the exception handler for this task.
|
AgentExecutionService.Task<T> |
setLogger(Logger logger)
Sets the logger for this task.
|
AgentExecutionService.Task<T> |
setLogLevel(Level level)
Sets the logging level for this task.
|
AgentExecutionService.Task<T> |
setName(String name)
Sets the name of this task.
|
AgentExecutionService.Task<T> |
setRestart(int maxAttempts,
long time,
TimeUnit unit)
Sets the restart policy for this task.
|
AgentExecutionService.Task<T> |
start()
Starts the task execution.
|
public String getName()
public Logger getLogger()
public Level getLogLevel()
public AgentExecutionService.Task<T> setName(String name)
name - Task name.IllegalStateException - if the task has already been started.public AgentExecutionService.Task<T> setExceptionHandler(AgentExecutionService.ExceptionHandler exceptionHandler)
exceptionHandler - Exception handler to be used by this task.IllegalStateException - if the task has already been started.public AgentExecutionService.Task<T> setLogger(Logger logger)
logger - Logger.IllegalStateException - if the task has already been started.public AgentExecutionService.Task<T> setLogLevel(Level level)
level - Level.IllegalStateException - if the task has already been started.public AgentExecutionService.Task<T> setRestart(int maxAttempts, long time, TimeUnit unit)
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 returns false. The value of this parameter
should not exceed 100.time - Length of time period for counting restarts. If zero, restarts are counted
towards maxAttempts throughout the lifetime of the task.unit - Unit for time period.IllegalStateException - if the task has already been started.IllegalArgumentException - if maxAttempts is above 100 or time is negative.public AgentExecutionService.Task<T> start()
RejectedExecutionException - if the task cannot be accepted for executionpublic boolean cancel(boolean mayInterruptIfRunning)
cancel is called, this task should never
run. If the task has already started, then the mayInterruptIfRunning parameter 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 return true.
Subsequent calls to isCancelled() will always return true if this method returned true.
cancel in interface Future<T>mayInterruptIfRunning - true if the thread executing
this task should be interrupted; otherwise,
in-progress tasks are allowed to complete.false if the task could not be canceled, typically
because it has already completed normally; true otherwise.public boolean isCancelled()
isCancelled in interface Future<T>true if this task was canceled.public boolean isDone()
true if this task completed.
Completion may be due to normal termination, an exception, or cancellation
- in all of these cases, this method will return true.public T get() throws InterruptedException, ExecutionException
get in interface Future<T>CancellationException - if the computation was canceled.ExecutionException - if the computation threw an exception.InterruptedException - if the current thread was interrupted while waiting.public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get in interface Future<T>timeout - Maximum time to wait.unit - Time unit of the timeout argument.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.Copyright © 2020 LSST. All rights reserved.