Package org.lsst.ccs.services
Class AgentExecutionService
java.lang.Object
java.util.concurrent.AbstractExecutorService
org.lsst.ccs.services.AgentExecutionService
- All Implemented Interfaces:
Executor,ExecutorService,HasLifecycle,ServiceLifecycle,AgentMonitor,AgentService
public class AgentExecutionService
extends AbstractExecutorService
implements ServiceLifecycle, HasLifecycle, AgentMonitor, AgentService
Service for executing asynchronous tasks.
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);
}
- Author:
- onoprien
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceImplemented by classes that provide custom handling for uncaught exceptions thrown byAgentExecutionServicetasks.classRepresents a customizable task to be executed byAgentExecutionService. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanawaitTermination(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.voidExecutes the given command on a thread provided by this service.Life cycle method, not for use by clients.getAgentMonitorStatus(boolean useCcsBuses) Life cycle method, not for use by clients.Life cycle method, not for use by clients.booleanReturnstrueif this service has been shut down.booleanReturnstrueif all tasks have completed following shut down.voidLife cycle method, not for use by clients.voidpreBuild()Life cycle method, not for use by clients.voidshutdown()Does nothing.Does nothing.Creates a task that will execute the provided command when started.<T> AgentExecutionService.Task<T>Creates a task that will execute the provided command and return the provided result when started.Creates a task that will execute the providedCallableand return its result when started.Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submitMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.lsst.ccs.services.AgentService
startForAgentMethods inherited from interface org.lsst.ccs.framework.HasLifecycle
build, init, postBuild, postInit, postStart, startMethods inherited from interface org.lsst.ccs.ServiceLifecycle
afterBuild, afterInit, afterStart, preInit, preShutdown, preStart
-
Constructor Details
-
AgentExecutionService
public AgentExecutionService()
-
-
Method Details
-
getAgentServiceName
Life cycle method, not for use by clients.- Specified by:
getAgentServiceNamein interfaceAgentService- Returns:
- the AgentService name.
-
preBuild
public void preBuild()Life cycle method, not for use by clients.- Specified by:
preBuildin interfaceServiceLifecycle
-
postShutdown
public void postShutdown()Life cycle method, not for use by clients.- Specified by:
postShutdownin interfaceHasLifecycle
-
execute
Executes the given command on a thread provided by this service. -
isShutdown
public boolean isShutdown()Returnstrueif this service has been shut down.- Specified by:
isShutdownin interfaceExecutorService- Returns:
- True if this service has been shut down.
-
isTerminated
public boolean isTerminated()Returnstrueif all tasks have completed following shut down.- Specified by:
isTerminatedin interfaceExecutorService- Returns:
- True if all tasks have completed following shut down.
-
shutdown
public void shutdown()Does nothing. This service is managed by theAgent.- Specified by:
shutdownin interfaceExecutorService- Specified by:
shutdownin interfaceHasLifecycle
-
shutdownNow
Does nothing. This service is managed by theAgent.- Specified by:
shutdownNowin interfaceExecutorService- Returns:
- Empty list.
-
awaitTermination
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.- Specified by:
awaitTerminationin interfaceExecutorService- Parameters:
timeout- Maximum time to wait.unit- Time unit of the timeout argument.- Returns:
- True if the executor terminated and false if the timeout elapsed before termination.
- Throws:
InterruptedException- If interrupted while waiting.
-
task
Creates a task that will execute the provided command 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:... AgentExecutionService service = agent.getAgentService(AgentExecutionService.class); service.task(command).start(); ...- Parameters:
runnable- Command to execute.- Returns:
- New task.
-
task
Creates a task that will execute the provided command and return the provided 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:- Type Parameters:
T- Type of the result.- Parameters:
runnable- Command to execute.result- Result to return.- Returns:
- New task.
-
task
Creates a task that will execute the providedCallableand 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:- Type Parameters:
T- Type of the result.- Parameters:
callable- Callable to execute.- Returns:
- New task.
-
getAgentMonitorStatus
Life cycle method, not for use by clients.- Specified by:
getAgentMonitorStatusin interfaceAgentMonitor- Parameters:
useCcsBuses- 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.- Returns:
- The Status of the AgentMonitor.
-
getAgentMonitorDescription
Life cycle method, not for use by clients.- Specified by:
getAgentMonitorDescriptionin interfaceAgentMonitor- Returns:
- The description for this AgentMonitor.
-