Interface MappingBatchInterceptor
- All Superinterfaces:
BatchInterceptor, BiFunction<MessageBatch, Tracker, MessageBatch>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface
public interface MappingBatchInterceptor
extends BatchInterceptor, BiFunction<MessageBatch, Tracker, MessageBatch>
A
BatchInterceptor specialization that transforms a MessageBatch before it is passed to the
consumer for processing.
This interface combines BatchInterceptor with a BiFunction contract, allowing implementors to
declaratively map a batch in-place:
- Filter out or modify messages within the batch
- Augment batch metadata or headers
- Replace or rewrap the entire batch
Design:
- This is a functional interface. Implementors only need to define
apply(MessageBatch, Tracker). - The
intercept(Consumer, Tracker)method delegates directly to theapply(...)transformation before passing the result to the next consumer. - Provides a clean entry point for small, functional batch manipulation use cases.
Example Usage
MappingBatchInterceptor maskingInterceptor = (batch, tracker) -> {
List<Message> maskedMessages = batch.getMessages().stream()
.map(m -> m.withMetadata(m.getMetadata().and("masked", true)))
.toList();
return batch.withMessages(maskedMessages);
};
ConsumerConfiguration.builder()
.name("maskedConsumer")
.batchInterceptor(maskingInterceptor)
.build();
Best Practices:
- For non-transformational logic (like delaying or thread context management), prefer
BatchInterceptordirectly. - Use
MappingBatchInterceptorwhen your logic results in a modified or filtered batch.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionapply(MessageBatch messageBatch, Tracker tracker) Applies a transformation to the givenMessageBatch, optionally modifying its contents or structure.default Consumer<MessageBatch> intercept(Consumer<MessageBatch> consumer, Tracker tracker) Wraps the batch processing consumer with a transformation step that rewrites the batch before processing.Methods inherited from interface BatchInterceptor
andThen, shutdownMethods inherited from interface BiFunction
andThen
-
Method Details
-
apply
Applies a transformation to the givenMessageBatch, optionally modifying its contents or structure.- Specified by:
applyin interfaceBiFunction<MessageBatch, Tracker, MessageBatch>- Parameters:
messageBatch- the incoming message batchtracker- the tracker handling the batch- Returns:
- the transformed batch to be passed to the next consumer
-
intercept
Wraps the batch processing consumer with a transformation step that rewrites the batch before processing.- Specified by:
interceptin interfaceBatchInterceptor- Parameters:
consumer- the original consumer that processes theMessageBatchtracker- the tracker invoking this interceptor- Returns:
- a wrapped consumer with additional behavior
-