E
- task payload type.public interface WorkQueue<E> extends DistributedPrimitive
Work queue serves as a buffer allowing producers to add
tasks and consumers
to take
tasks to process.
In the system each task is tracked via its unique task identifier which is returned when a task is taken.
Work queue guarantees that a task can be taken by only one consumer at a time. Once it finishes processing a
consumer must invoke the complete
method to mark the task(s) as completed.
Tasks thus completed are removed from the queue. If a consumer unexpectedly terminates before it can complete
all its tasks are returned back to the queue so that other consumers can pick them up. Since there is a distinct
possibility that tasks could be processed more than once (under failure conditions), care should be taken to ensure
task processing logic is idempotent.
DistributedPrimitive.Status, DistributedPrimitive.Type
DEFAULT_OPERATION_TIMEOUT_MILLIS, DEFAULT_OPERTATION_TIMEOUT_MILLIS
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.CompletableFuture<java.lang.Void> |
addMultiple(java.util.Collection<E> items)
Adds a collection of tasks to the work queue.
|
default java.util.concurrent.CompletableFuture<java.lang.Void> |
addOne(E item)
Adds a single task to the work queue.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
complete(java.util.Collection<java.lang.String> taskIds)
Completes a collection of tasks.
|
default java.util.concurrent.CompletableFuture<java.lang.Void> |
complete(java.lang.String... taskIds)
Completes a collection of tasks.
|
default DistributedPrimitive.Type |
primitiveType()
Returns the type of primitive.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
registerTaskProcessor(java.util.function.Consumer<E> taskProcessor,
int parallelism,
java.util.concurrent.Executor executor)
Registers a task processing callback to be automatically invoked when new tasks are
added to the work queue.
|
java.util.concurrent.CompletableFuture<WorkQueueStats> |
stats()
Returns work queue statistics.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
stopProcessing()
Stops automatically processing tasks from work queue.
|
default java.util.concurrent.CompletableFuture<Task<E>> |
take()
Picks up a single task from the work queue to work on.
|
java.util.concurrent.CompletableFuture<java.util.Collection<Task<E>>> |
take(int maxItems)
Picks up multiple tasks from the work queue to work on.
|
addStatusChangeListener, applicationId, destroy, name, removeStatusChangeListener, statusChangeListeners
default DistributedPrimitive.Type primitiveType()
DistributedPrimitive
primitiveType
in interface DistributedPrimitive
java.util.concurrent.CompletableFuture<java.lang.Void> addMultiple(java.util.Collection<E> items)
items
- collection of task itemsjava.util.concurrent.CompletableFuture<java.util.Collection<Task<E>>> take(int maxItems)
Tasks that are taken remain invisible to other consumers as long as the consumer stays alive.
If a consumer unexpectedly terminates before completing
the task,
the task becomes visible again to other consumers to process.
maxItems
- maximum number of items to take from the queue. The actual number of tasks returned
can be at the max this numberjava.util.concurrent.CompletableFuture<java.lang.Void> complete(java.util.Collection<java.lang.String> taskIds)
taskIds
- ids of tasks to completejava.util.concurrent.CompletableFuture<java.lang.Void> registerTaskProcessor(java.util.function.Consumer<E> taskProcessor, int parallelism, java.util.concurrent.Executor executor)
taskProcessor
- task processing callbackparallelism
- max tasks that can be processed in parallelexecutor
- executor to use for processing the tasksjava.util.concurrent.CompletableFuture<java.lang.Void> stopProcessing()
registerTaskProcessor
call.java.util.concurrent.CompletableFuture<WorkQueueStats> stats()
default java.util.concurrent.CompletableFuture<java.lang.Void> complete(java.lang.String... taskIds)
taskIds
- var arg list of task idsdefault java.util.concurrent.CompletableFuture<java.lang.Void> addOne(E item)
item
- task itemdefault java.util.concurrent.CompletableFuture<Task<E>> take()
Tasks that are taken remain invisible to other consumers as long as the consumer stays alive.
If a consumer unexpectedly terminates before completing
the task,
the task becomes visible again to other consumers to process.