Package com.lucimber.dbus.util
Class MessageBatcher
java.lang.Object
com.lucimber.dbus.util.MessageBatcher
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 Summary
ConstructorsConstructorDescriptionCreates a new message batcher with default configuration.MessageBatcher
(int maxBatchSize, int maxBatchBytes, Duration batchDelay) Creates a new message batcher with custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionboolean
addMessage
(io.netty.channel.ChannelHandlerContext ctx, OutboundMessage message, int estimatedSize) Adds a message to the current batch.flushBatch
(io.netty.channel.ChannelHandlerContext ctx) Flushes the current batch immediately.Gets performance metrics for the batcher.void
Resets all metrics.void
shutdown()
Shuts down the batcher, canceling any pending flushes.
-
Constructor Details
-
MessageBatcher
public MessageBatcher()Creates a new message batcher with default configuration. -
MessageBatcher
Creates a new message batcher with custom configuration.- Parameters:
maxBatchSize
- maximum messages per batchmaxBatchBytes
- maximum bytes per batchbatchDelay
- 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 contextmessage
- the message to batchestimatedSize
- estimated size of the message in bytes- Returns:
- true if the message was batched, false if it should be sent immediately
-
flushBatch
Flushes the current batch immediately.- Parameters:
ctx
- channel handler context- Returns:
- list of messages to send, or empty list if batch is empty
-
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.
-