Class Backlog<T>
java.lang.Object
io.fluxzero.common.Backlog<T>
- Type Parameters:
T- The type of item being buffered and processed.
A thread-safe batching queue that asynchronously flushes its content to a consumer in configurable batch sizes.
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) -
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.protected voidcompleteResults(long untilPosition, Throwable e) 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, ErrorHandler<List<T>> errorHandler) Creates a backlog for an asynchronous consumer with custom max batch size and error handler.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.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)
-
-
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. -
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.
-
completeResults
-
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.
-