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
  • Constructor Details

    • AgentExecutionService

      public AgentExecutionService()
  • Method Details

    • getAgentServiceName

      public String getAgentServiceName()
      Life cycle method, not for use by clients.
      Specified by:
      getAgentServiceName in interface AgentService
      Returns:
      the AgentService name.
    • preBuild

      public void preBuild()
      Life cycle method, not for use by clients.
      Specified by:
      preBuild in interface ServiceLifecycle
    • postShutdown

      public void postShutdown()
      Life cycle method, not for use by clients.
      Specified by:
      postShutdown in interface HasLifecycle
    • execute

      public void execute(Runnable command)
      Executes the given command on a thread provided by this service.
      Specified by:
      execute in interface Executor
      Parameters:
      command - Runnable task.
    • isShutdown

      public boolean isShutdown()
      Returns true if this service has been shut down.
      Specified by:
      isShutdown in interface ExecutorService
      Returns:
      True if this service has been shut down.
    • isTerminated

      public boolean isTerminated()
      Returns true if all tasks have completed following shut down.
      Specified by:
      isTerminated in interface ExecutorService
      Returns:
      True if all tasks have completed following shut down.
    • shutdown

      public void shutdown()
      Does nothing. This service is managed by the Agent.
      Specified by:
      shutdown in interface ExecutorService
      Specified by:
      shutdown in interface HasLifecycle
    • shutdownNow

      public List<Runnable> shutdownNow()
      Does nothing. This service is managed by the Agent.
      Specified by:
      shutdownNow in interface ExecutorService
      Returns:
      Empty list.
    • awaitTermination

      public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
      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:
      awaitTermination in interface ExecutorService
      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

      public AgentExecutionService.Task task(Runnable runnable)
      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

      public <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. 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

      public <T> AgentExecutionService.Task task(Callable<T> callable)
      Creates a task that will execute the provided 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:
      Type Parameters:
      T - Type of the result.
      Parameters:
      callable - Callable to execute.
      Returns:
      New task.
    • getAgentMonitorStatus

      public String getAgentMonitorStatus(boolean useCcsBuses)
      Life cycle method, not for use by clients.
      Specified by:
      getAgentMonitorStatus in interface AgentMonitor
      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

      public String getAgentMonitorDescription()
      Life cycle method, not for use by clients.
      Specified by:
      getAgentMonitorDescription in interface AgentMonitor
      Returns:
      The description for this AgentMonitor.