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

public class AgentExecutionService.Task<T> extends Object implements Future<T>
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 Details

    • getName

      public String getName()
      Returns the name of this task.
      Returns:
      Task name.
    • getLogger

      public Logger getLogger()
      Returns the logger for this task.
      Returns:
      Logger.
    • getLogLevel

      public Level getLogLevel()
      Returns the log level for this task.
      Returns:
      Log level.
    • setName

      public AgentExecutionService.Task<T> setName(String name)
      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

      public AgentExecutionService.Task<T> setLogger(Logger logger)
      Sets the logger for this task.
      Parameters:
      logger - Logger.
      Returns:
      This task.
      Throws:
      IllegalStateException - if the task has already been started.
    • setLogLevel

      public AgentExecutionService.Task<T> setLogLevel(Level level)
      Sets the logging level for this task.
      Parameters:
      level - Level.
      Returns:
      This task.
      Throws:
      IllegalStateException - if the task has already been started.
    • setRestart

      public AgentExecutionService.Task<T> setRestart(int maxAttempts, long time, TimeUnit unit)
      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 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.
      Returns:
      This task.
      Throws:
      IllegalStateException - if the task has already been started.
      IllegalArgumentException - if maxAttempts is above 100 or time is negative.
    • start

      public AgentExecutionService.Task<T> 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 when 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.

      Specified by:
      cancel in interface Future<T>
      Parameters:
      mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.
      Returns:
      false if the task could not be canceled, typically because it has already completed normally; true otherwise.
    • isCancelled

      public boolean isCancelled()
      Checks whether this task was canceled before it completed normally.
      Specified by:
      isCancelled in interface Future<T>
      Returns:
      true if this task was canceled.
    • isDone

      public boolean isDone()
      Returns 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.
      Specified by:
      isDone in interface Future<T>
      Returns:
      true if this task completed.
    • get

      Waits if necessary for the computation to complete, and then retrieves its result.
      Specified by:
      get in interface Future<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:
      get in interface Future<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.