Class StallingBatchInterceptor
java.lang.Object
io.fluxzero.sdk.tracking.StallingBatchInterceptor
- All Implemented Interfaces:
BatchInterceptor
A
BatchInterceptor that stalls batch processing until a minimum desired batch size is reached or a timeout occurs.
This interceptor helps regulate the trade-off between **throughput** and **latency** by introducing intentional delays when batches are too small. It’s especially useful in cases where:
- Handlers benefit from larger batches (e.g., bulk writes, deduplication, aggregation)
- The event rate is low and batching is desirable
Behavior
- If the batch size is greater than or equal to
desiredBatchSize, it is processed immediately. - If the batch size is too small:
- The interceptor delays processing using
Thread.sleepin intervals ofretryFrequency. - Once
maximumStallingDurationhas elapsed since the first refusal, the batch is processed regardless of size.
- The interceptor delays processing using
Usage Considerations
- This interceptor causes blocking in the tracker thread. It is meant for controlled environments where latency can be traded for efficiency.
- It is thread-safe and maintains its own internal stall timer across batches using an
AtomicReference.
Example Usage
ConsumerConfiguration.builder()
.name("batchedHandler")
.batchInterceptor(StallingBatchInterceptor.builder()
.desiredBatchSize(100)
.maximumStallingDuration(Duration.ofSeconds(30))
.retryFrequency(Duration.ofMillis(500))
.build())
.build();
Defaults
desiredBatchSize= 512maximumStallingDuration= 60 secondsretryFrequency= 1 second
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanintercept(Consumer<MessageBatch> consumer, Tracker tracker) Intercepts the given batch message consumer and returns a decorated version to be invoked by the tracker.protected voidstall()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface BatchInterceptor
andThen, shutdown
-
Constructor Details
-
StallingBatchInterceptor
public StallingBatchInterceptor()
-
-
Method Details
-
intercept
Description copied from interface:BatchInterceptorIntercepts the given batch message consumer and returns a decorated version to be invoked by the tracker.- Specified by:
interceptin interfaceBatchInterceptor- Parameters:
consumer- the original consumer that processes theMessageBatchtracker- the tracker invoking this interceptor- Returns:
- a wrapped consumer with additional behavior
-
hasPassedDeadline
protected boolean hasPassedDeadline() -
stall
protected void stall()
-