Class BlockingTaskQueue


  • public class BlockingTaskQueue
    extends java.lang.Object
    Helps to manage threading for a series of tasks. These tasks are blocking tasks that may produce a value (T) and ideally can gracefully handle being cancelled.

    This class will guarantee:

    1. Only one task executes at a time.
    2. All tasks execute on the executor given.
    3. Tasks will have their BlockingCancelableTask.cancel() called if they exceed the configured timeout, and their completion stage will be completed exceptionally with a TaskTimedOutException
    4. Tasks submitted while other tasks are executing are put into a queue. If the queue fills up, the oldest tasks are rejected first. Rejected tasks will have their completion stages completed exceptionally with a QueueOverflowException
    • Method Detail

      • submit

        public <T> java.util.concurrent.CompletionStage<T> submit​(BlockingCancelableTask<T> work)
        Submit a task to the queue.
        Returns:
        CompletionStage that will be completed with the return value of type T produced by the task, or completed exceptionally if the task times out, the task is kicked out of the queue by more tasks, or the task throws an exception during execution.
        Throws:
        QueueShutdownException - if this queue has been shutdown.
      • shutdown

        public void shutdown()
        Shuts down the queue. Any excuting or pending tasks will be canceled, their completion stages completed exceptionally with a QueueShutdownException.
      • newBuilder

        public static BlockingTaskQueue.Builder newBuilder()
        Create a new blocking task queue builder with a default of 30 second timeout and max queue length of 5