Class StallingBatchInterceptor

java.lang.Object
io.fluxcapacitor.javaclient.tracking.StallingBatchInterceptor
All Implemented Interfaces:
BatchInterceptor

public class StallingBatchInterceptor extends Object implements 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.sleep in intervals of retryFrequency.
    • Once maximumStallingDuration has elapsed since the first refusal, the batch is processed regardless of size.

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 = 512
  • maximumStallingDuration = 60 seconds
  • retryFrequency = 1 second
See Also:
  • Constructor Details

    • StallingBatchInterceptor

      public StallingBatchInterceptor()
  • Method Details

    • intercept

      public Consumer<MessageBatch> intercept(Consumer<MessageBatch> consumer, Tracker tracker)
      Description copied from interface: BatchInterceptor
      Intercepts the given batch message consumer and returns a decorated version to be invoked by the tracker.
      Specified by:
      intercept in interface BatchInterceptor
      Parameters:
      consumer - the original consumer that processes the MessageBatch
      tracker - the tracker invoking this interceptor
      Returns:
      a wrapped consumer with additional behavior
    • hasPassedDeadline

      protected boolean hasPassedDeadline()
    • stall

      protected void stall()