Class Backlog<T>
- Type Parameters:
T- The type of item being buffered and processed.
This utility is useful for scenarios where multiple values are being added over time and you want to consume them in batches for efficiency—such as sending messages to a remote system, writing to a log, etc.
Flushes are executed on a single background thread, and results (e.g. completion or failure) are tracked
via CompletableFutures. Optional monitors may observe each flushed batch.
Key Features
- Supports both synchronous and asynchronous consumers
- Flushes automatically after new items are added
- Tracks flush progress with
CompletableFutureper add - Customizable error handling via
ErrorHandler - Monitoring support via
Monitored
Typical Use
Backlog<String> backlog = Backlog.forAsyncConsumer(batch -> {
return sendToServer(batch); // returns CompletableFuture
});
backlog.add("a", "b", "c");
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA function that consumes a batch of items and returns a future that completes when processing is done. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBacklog(ThrowingFunction<List<T>, CompletableFuture<?>> consumer) protectedBacklog(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize) protectedBacklog(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) protectedBacklog(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler, int maxInFlightBatches) -
Method Summary
Modifier and TypeMethodDescriptionadd(Collection<? extends T> values) Adds a collection of values to the backlog.final CompletableFuture<Void> Adds values to the backlog.voidaddAllUntracked(Collection<? extends T> values) Adds multiple values without creating per-value flush futures.voidaddUntracked(T value) Adds one value without allocating a separate flush future.static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer) Creates a backlog for an asynchronous consumer with default max batch size and default logging error handler.static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize) Creates a backlog for an asynchronous consumer with custom max batch size and default logging error handler.static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, int maxInFlightBatches) Creates a backlog for an asynchronous consumer with a bounded number of in-flight batches.static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, int maxInFlightBatches, ErrorHandler<List<T>> errorHandler) Creates a bounded asynchronous backlog with a custom error handler.static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) Creates a backlog for an asynchronous consumer with custom max batch size and error handler.static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> itemWeight, long maxBatchWeight, int maxInFlightBatches) Creates an asynchronous backlog bounded by item count, cumulative item weight and in-flight batches.static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> itemWeight, long maxBatchWeight, int maxInFlightBatches, Duration batchCollectionDelay) Creates an asynchronous backlog bounded by item count, cumulative item weight and in-flight batches, with a bounded collection delay whenever an idle backlog starts flushing.static <T> Backlog<T> forConsumer(ThrowingConsumer<List<T>> consumer) Creates a new backlog for a synchronous consumer and default batch size and default logging error handler.static <T> Backlog<T> forConsumer(ThrowingConsumer<List<T>> consumer, int maxBatchSize) Creates a backlog with custom max batch size and default logging error handler.static <T> Backlog<T> forConsumer(ThrowingConsumer<List<T>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) Creates a backlog with custom max batch size and error handler.static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer) Deprecated, for removal: This API element is subject to removal in a future version.static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, int)with one in-flight batch.static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, int, ErrorHandler)with one in-flight batch.static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> batchWeight, long maxBatchWeight) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, ToLongFunction, long, int)with one in-flight batch.static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> batchWeight, long maxBatchWeight, Duration batchCollectionDelay) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, ToLongFunction, long, int, Duration)with one in-flight batch.registerMonitor(Consumer<List<T>> monitor) Adds a monitor to observe flushed batches.voidshutDown()Shuts down the internal executor service cleanly.
-
Constructor Details
-
Backlog
-
Backlog
-
Backlog
protected Backlog(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) -
Backlog
protected Backlog(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler, int maxInFlightBatches)
-
-
Method Details
-
forConsumer
Creates a new backlog for a synchronous consumer and default batch size and default logging error handler. -
forConsumer
Creates a backlog with custom max batch size and default logging error handler. -
forConsumer
public static <T> Backlog<T> forConsumer(ThrowingConsumer<List<T>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) Creates a backlog with custom max batch size and error handler. -
forAsyncConsumer
public static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer) Creates a backlog for an asynchronous consumer with default max batch size and default logging error handler. -
forAsyncConsumer
public static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize) Creates a backlog for an asynchronous consumer with custom max batch size and default logging error handler. -
forAsyncConsumer
public static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) Creates a backlog for an asynchronous consumer with custom max batch size and error handler. -
forOrderedAsyncConsumer
@Deprecated(forRemoval=true) public static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, int)with one in-flight batch. -
forOrderedAsyncConsumer
@Deprecated(forRemoval=true) public static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, int)with one in-flight batch. -
forOrderedAsyncConsumer
@Deprecated(forRemoval=true) public static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ErrorHandler<List<T>> errorHandler) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, int, ErrorHandler)with one in-flight batch. -
forOrderedAsyncConsumer
@Deprecated(forRemoval=true) public static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> batchWeight, long maxBatchWeight) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, ToLongFunction, long, int)with one in-flight batch. -
forOrderedAsyncConsumer
@Deprecated(forRemoval=true) public static <T> Backlog<T> forOrderedAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> batchWeight, long maxBatchWeight, Duration batchCollectionDelay) Deprecated, for removal: This API element is subject to removal in a future version.UseforAsyncConsumer(ThrowingFunction, int, ToLongFunction, long, int, Duration)with one in-flight batch. -
forAsyncConsumer
public static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, int maxInFlightBatches) Creates a backlog for an asynchronous consumer with a bounded number of in-flight batches. A batch remains in flight until the future returned by the consumer completes. A new batch is dispatched as soon as capacity becomes available. -
forAsyncConsumer
public static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, int maxInFlightBatches, ErrorHandler<List<T>> errorHandler) Creates a bounded asynchronous backlog with a custom error handler. -
forAsyncConsumer
public static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> itemWeight, long maxBatchWeight, int maxInFlightBatches) Creates an asynchronous backlog bounded by item count, cumulative item weight and in-flight batches.The first item is always admitted, even when it exceeds
maxBatchWeight, so an oversized item can make progress as a one-item batch.- Parameters:
consumer- asynchronous batch consumermaxBatchSize- maximum number of items in one batchitemWeight- function that returns the non-negative weight of an itemmaxBatchWeight- maximum cumulative weight, except for one individually oversized itemmaxInFlightBatches- maximum number of consumer batches whose returned future has not completed
-
forAsyncConsumer
public static <T> Backlog<T> forAsyncConsumer(ThrowingFunction<List<T>, CompletableFuture<?>> consumer, int maxBatchSize, ToLongFunction<? super T> itemWeight, long maxBatchWeight, int maxInFlightBatches, Duration batchCollectionDelay) Creates an asynchronous backlog bounded by item count, cumulative item weight and in-flight batches, with a bounded collection delay whenever an idle backlog starts flushing.The delay only applies to the first batch after the backlog was idle. Batches already queued behind an active consumer are drained immediately. This allows very short micro-batching windows without delaying a sustained backlog once it has filled.
- Parameters:
consumer- asynchronous batch consumermaxBatchSize- maximum number of items in one batchitemWeight- function that returns the non-negative weight of an itemmaxBatchWeight- maximum cumulative weight, except for one individually oversized itemmaxInFlightBatches- maximum number of consumer batches whose returned future has not completedbatchCollectionDelay- maximum time to collect concurrent items after an idle start
-
add
Adds values to the backlog.- Parameters:
values- one or more values to enqueue- Returns:
- a future that completes when the values are processed by the consumer.
-
add
Adds a collection of values to the backlog.- Parameters:
values- collection of values to enqueue- Returns:
- a future that completes when the values are processed by the consumer.
-
addUntracked
Adds one value without allocating a separate flush future.Use this only when the asynchronous consumer owns completion and failure propagation for the value itself. Consumer failures still reach the configured backlog error handler.
-
addAllUntracked
Adds multiple values without creating per-value flush futures.- See Also:
-
registerMonitor
Adds a monitor to observe flushed batches.- Specified by:
registerMonitorin interfaceMonitored<T>- Parameters:
monitor- the observer- Returns:
- a
Registrationthat can be used to remove the monitor
-
shutDown
public void shutDown()Shuts down the internal executor service cleanly.
-
forAsyncConsumer(ThrowingFunction, int, int)with one in-flight batch.