
public class AgentExecutionService extends AbstractExecutorService implements ServiceLifecycle, HasLifecycle, AgentMonitor, AgentService
This service implements ExecutorService and can used wherever an executor is needed,
without worrying about configuring it or shutting it down. All threads are daemon threads.
The number of threads is unlimited, idle threads are kept alive for 70 seconds.
Additional error handling customization, monitoring, and task control capabilities are available
through the AgentExecutionService.Task class. Below are a few examples of use:
To launch a Runnable on a dedicated, explicitly named thread, and make sure a message is
logged if it throws an exception:
...
AgentExecutionService service = agent.getAgentService(AgentExecutionService.class);
service.task(runnable).setName("Background task").start();
...
To submit a Callable and keep restarting it until it either succeeds, or is canceled,
or fails more than 5 times within 1 minute, and disable log messages:
...
AgentExecutionService service = agent.getAgentService(AgentExecutionService.class);
AgentExecutionService.Task<String> task = service.task(callable)
.setName("stubborn task")
.setRestart(5, 1, TimeUnit.MINUTES)
.setExceptionHandler((task, throwable, willRestart) -> true)
.start();
...
// Perhaps on some other thread, wait for the result:
try {
String output = task.get(10, TimeUnit.DAYS);
...
} catch (TimeoutException) {
task.cancel(true);
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
AgentExecutionService.ExceptionHandler
Implemented by classes that provide custom handling for uncaught exceptions thrown by
AgentExecutionService tasks. |
class |
AgentExecutionService.Task<T>
Represents a customizable task to be executed by
AgentExecutionService. |
| Constructor and Description |
|---|
AgentExecutionService() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs,
or the current thread is interrupted, whichever happens first.
|
void |
execute(Runnable command)
Executes the given command on a thread provided by this service.
|
String |
getAgentMonitorDescription()
Life cycle method, not for use by clients.
|
String |
getAgentMonitorStatus(boolean useCcsBuses)
Life cycle method, not for use by clients.
|
String |
getAgentServiceName()
Life cycle method, not for use by clients.
|
boolean |
isShutdown()
Returns
true if this service has been shut down. |
boolean |
isTerminated()
Returns
true if all tasks have completed following shut down. |
void |
postShutdown()
Life cycle method, not for use by clients.
|
void |
preBuild()
Life cycle method, not for use by clients.
|
void |
shutdown()
Does nothing.
|
List<Runnable> |
shutdownNow()
Does nothing.
|
<T> AgentExecutionService.Task |
task(Callable<T> callable)
Creates a task that will execute the provided
Callable and return its result when started. |
AgentExecutionService.Task |
task(Runnable runnable)
Creates a task that will execute the provided command when started.
|
<T> AgentExecutionService.Task<T> |
task(Runnable runnable,
T result)
Creates a task that will execute the provided command and return the provided result when started.
|
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submitclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitafterInit, afterStart, preInit, preShutdown, preStartbuild, init, postBuild, postInit, postStart, startstartForAgentpublic String getAgentServiceName()
getAgentServiceName in interface AgentServicepublic void preBuild()
preBuild in interface ServiceLifecyclepublic void postShutdown()
postShutdown in interface HasLifecyclepublic void execute(Runnable command)
public boolean isShutdown()
true if this service has been shut down.isShutdown in interface ExecutorServicepublic boolean isTerminated()
true if all tasks have completed following shut down.isTerminated in interface ExecutorServicepublic void shutdown()
Agent.shutdown in interface ExecutorServiceshutdown in interface HasLifecyclepublic List<Runnable> shutdownNow()
Agent.shutdownNow in interface ExecutorServicepublic boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface ExecutorServicetimeout - Maximum time to wait.unit - Time unit of the timeout argument.InterruptedException - If interrupted while waiting.public AgentExecutionService.Task task(Runnable runnable)
...
AgentExecutionService service = agent.getAgentService(AgentExecutionService.class);
service.task(command).start();
...
runnable - Command to execute.public <T> AgentExecutionService.Task<T> task(Runnable runnable, T result)
T - Type of the result.runnable - Command to execute.result - Result to return.public <T> AgentExecutionService.Task task(Callable<T> callable)
Callable and return its result when started.
The task can customized before execution if necessary, or simply started to execute the command
on a thread provided by this service and log a message at INFO level if it throws an exception:T - Type of the result.callable - Callable to execute.public String getAgentMonitorStatus(boolean useCcsBuses)
getAgentMonitorStatus in interface AgentMonitoruseCcsBuses - a boolean to tell if the CCS buses should be used: for
example to raise an Alert. This is true when the method is invoked from
the periodic task. If it's invoked form JMX its value is false.public String getAgentMonitorDescription()
getAgentMonitorDescription in interface AgentMonitorCopyright © 2021 LSST. All rights reserved.