Class SerialTaskQueue

java.lang.Object
com.couchbase.client.core.util.SerialTaskQueue

@Internal public final class SerialTaskQueue extends Object
Runs asynchronous tasks one at a time, in submission order.

A task is a Supplier<Mono<Void>>. The next task does not start until the Mono returned by the previous task terminates. Tasks run on the given scheduler.

Cancelling the Mono returned by submit(java.util.function.Supplier<reactor.core.publisher.Mono<java.lang.Void>>) before the task starts means the task is skipped. Cancelling after the task starts has no effect on the task; it still runs to completion before the next task starts.

A task must not wait for the completion of another task submitted to the same queue, or it will deadlock. Attempting to do so fails with IllegalStateException.

If the scheduler rejects work (for example, because it was disposed), every task waiting in the queue fails with the scheduler's exception instead of waiting forever. A task already running is not affected.

Each task's Mono is subscribed with a Reactor Context that identifies this queue. Code that must only run as part of a task can verify this with requireInTask().

  • Constructor Details

    • SerialTaskQueue

      public SerialTaskQueue(reactor.core.scheduler.Scheduler scheduler)
  • Method Details

    • submit

      public reactor.core.publisher.Mono<Void> submit(Supplier<reactor.core.publisher.Mono<Void>> work)
      Returns a Mono that, when subscribed, enqueues the task and completes (or fails) when the task's Mono terminates.
    • requireInTask

      public reactor.core.publisher.Mono<Void> requireInTask()
      Returns a Mono that completes if it is part of a task running on this queue, or fails with IllegalStateException otherwise.

      The check uses the Reactor Context, so it works regardless of which thread runs the task.