Class MessageBatcher

java.lang.Object
com.lucimber.dbus.util.MessageBatcher

public class MessageBatcher extends Object
Message batcher for improved throughput by grouping multiple small messages.

This batcher collects outbound messages and sends them in batches to reduce system call overhead and improve network utilization. It implements adaptive batching based on message rate and size.

Features:

  • Time-based batching with configurable delay
  • Size-based batching with configurable limits
  • Adaptive batch sizing based on throughput
  • Performance metrics tracking
  • Constructor Details

    • MessageBatcher

      public MessageBatcher()
      Creates a new message batcher with default configuration.
    • MessageBatcher

      public MessageBatcher(int maxBatchSize, int maxBatchBytes, Duration batchDelay)
      Creates a new message batcher with custom configuration.
      Parameters:
      maxBatchSize - maximum messages per batch
      maxBatchBytes - maximum bytes per batch
      batchDelay - maximum delay before flushing a batch
  • Method Details

    • addMessage

      public boolean addMessage(io.netty.channel.ChannelHandlerContext ctx, OutboundMessage message, int estimatedSize)
      Adds a message to the current batch.
      Parameters:
      ctx - channel handler context
      message - the message to batch
      estimatedSize - estimated size of the message in bytes
      Returns:
      true if the message was batched, false if it should be sent immediately
    • flushBatch

      public List<OutboundMessage> flushBatch(io.netty.channel.ChannelHandlerContext ctx)
      Flushes the current batch immediately.
      Parameters:
      ctx - channel handler context
      Returns:
      list of messages to send, or empty list if batch is empty
    • getMetrics

      public String getMetrics()
      Gets performance metrics for the batcher.
      Returns:
      formatted string with metrics
    • resetMetrics

      public void resetMetrics()
      Resets all metrics.
    • shutdown

      public void shutdown()
      Shuts down the batcher, canceling any pending flushes.