Interface Command<T>

Type Parameters:
T - the type of value that the operation will produce.
All Superinterfaces:
Callable<T>

public interface Command<T> extends Callable<T>
Wraps an operation which may be run synchronously. Prior to performing the operation, timeouts may be set to control how long the operation is allowed to run without producing output or processing input (the its idle timeout or to provide an absolute limit how long the operation is allowed to run (the execution timeout.

When the operation completes, it will return an object of type T. If T is Void, the operation will return null on completion. Typically this means the operation uses some form of callback, allowing the caller to process the results as they are produced rather than returning them on completion.

  • Method Summary

    Modifier and Type
    Method
    Description
    Transforms this Command into an AsyncCommand which may be called asynchronously.
    Executes a command
    void
    setExecutionTimeout(long timeoutInSecs)
    Sets the maximum time, in seconds, the operation is allowed to run.
    default void
    Sets the maximum duration the operation is allowed to run.
    void
    setIdleTimeout(long timeoutInSecs)
    Sets the maximum time, in seconds, the operation is allowed to run without either producing output or processing input.
    default void
    Sets the maximum duration the operation is allowed to run without either producing output or processing input.
    default void
    setTimeout(long timeoutInSecs)
    Sets the idle and execution timeouts, in seconds, for the operation.
    default void
    Sets the idle and execution timeouts to the specified duration.
  • Method Details

    • asynchronous

      @Nonnull AsyncCommand<T> asynchronous()
      Transforms this Command into an AsyncCommand which may be called asynchronously. Any timeouts applied this command will not be applied to the resulting AsyncCommand.

      Once an Command has been transformed for asynchronous use, it may no longer be used synchronously.

      Returns:
      an AsyncCommand for executing this Command asynchronously
      Throws:
      UnsupportedOperationException - if the implementation cannot internally convert the operation for asynchronous execution
    • call

      @Nullable T call()
      Executes a command
      Specified by:
      call in interface Callable<T>
      Returns:
      the result of the operation
      Throws:
      CommandCanceledException - if command execution is canceled
      ServiceException - if an error occurs while performing the operation
    • setExecutionTimeout

      void setExecutionTimeout(long timeoutInSecs)
      Sets the maximum time, in seconds, the operation is allowed to run. The operation will be aborted automatically if it hasn't completed in this time even if it is still producing output or processing input.
      Parameters:
      timeoutInSecs - the maximum time the operation is allowed to run, in seconds
      Throws:
      UnsupportedOperationException - if the implementation does not support configuring an execution timeout
    • setExecutionTimeout

      default void setExecutionTimeout(@Nonnull Duration timeout)
      Sets the maximum duration the operation is allowed to run. The operation will be aborted automatically if it hasn't completed in this time even if it is still producing output or processing input.
      Parameters:
      timeout - the maximum duration the operation is allowed to run
      Throws:
      UnsupportedOperationException - if the implementation does not support configuring an execution timeout
      Since:
      5.1
    • setIdleTimeout

      void setIdleTimeout(long timeoutInSecs)
      Sets the maximum time, in seconds, the operation is allowed to run without either producing output or processing input. Output may be produced on either the standard output or standard error streams.
      Parameters:
      timeoutInSecs - the maximum time the operation is allowed to be idle, in seconds
      Throws:
      UnsupportedOperationException - if the implementation does not support configuring an idle timeout
    • setIdleTimeout

      default void setIdleTimeout(@Nonnull Duration timeout)
      Sets the maximum duration the operation is allowed to run without either producing output or processing input. Output may be produced on either the standard output or standard error streams.
      Parameters:
      timeout - the maximum duration the operation is allowed to be idle
      Throws:
      UnsupportedOperationException - if the implementation does not support configuring an idle timeout
      Since:
      5.1
    • setTimeout

      default void setTimeout(long timeoutInSecs)
      Sets the idle and execution timeouts, in seconds, for the operation. This is useful for commands that do their processing up-front and may not produce any output until processing is complete. Setting the idle timeout to the same value as the execution timeout effectively disables the idle timeout.
      Parameters:
      timeoutInSecs - the maximum time the operation is allowed to run or be idle, in seconds
      Since:
      5.1
    • setTimeout

      default void setTimeout(@Nonnull Duration timeout)
      Sets the idle and execution timeouts to the specified duration. This is useful for commands that do their processing up-front and may not produce any output until processing is complete. Setting the idle timeout to the same value as the execution timeout effectively disables the idle timeout.
      Parameters:
      timeout - the maximum duration the operation is allowed to run or be idle
      Since:
      5.1