MockConsumerInterceptor.java
Overview
`MockConsumerInterceptor.java` defines a mock implementation of the Kafka `ConsumerInterceptor` interface, primarily used for testing and debugging purposes within the Apache Camel Kafka component. This interceptor captures all consumed Kafka records by storing them in a static list, allowing tests or debugging tools to inspect the records that have passed through the consumer. It logs each consumed record at the trace level but does not alter or filter any records, nor does it perform any actions on commit, close, or configuration.
This file facilitates monitoring and verification of Kafka consumer behavior in integration tests or during development without affecting the Kafka consumer processing pipeline.
Class: MockConsumerInterceptor
public class MockConsumerInterceptor implements ConsumerInterceptor<String, String>
Purpose
Implements Kafka's
ConsumerInterceptorinterface to intercept consumed messages.Captures and stores all consumed
ConsumerRecordsfor later inspection.Provides trace-level logging of each consumed record's value.
Does not modify records or participate in commit handling.
Fields
Field Name | Type | Description |
|---|---|---|
`recordsCaptured` | `public static ArrayList>` | A globally accessible list storing all intercepted consumer records. Enables external test code or tools to access consumed data. |
`LOG` | `private static final Logger` | Logger instance for logging record values at trace level. |
Methods
ConsumerRecords<String, String> onConsume(ConsumerRecords<String, String> consumerRecords)
Description: Called by Kafka when records are fetched by the consumer. This method intercepts the consumed records.
Parameters:
consumerRecords: The batch of records fetched from Kafka.
Returns: The same
consumerRecordsinstance received, unmodified.Behavior:
Iterates over each record in the batch and logs its value at the trace level.
Adds the entire
ConsumerRecordsbatch to the staticrecordsCapturedlist.
Usage Example:
// When Kafka consumer fetches records, onConsume is triggered:
ConsumerRecords<String, String> fetchedRecords = ...;
ConsumerRecords<String, String> processedRecords = mockInterceptor.onConsume(fetchedRecords);
// processedRecords == fetchedRecords (no changes)
void onCommit(Map<TopicPartition, OffsetAndMetadata> offsets)
Description: Called when offsets are committed.
Parameters:
offsets: Map of topic partitions to offset metadata.
Behavior: No operation (noop) in this mock implementation.
void close()
Description: Called when the interceptor is closed.
Behavior: No operation (noop).
void configure(Map<String, ?> configs)
Description: Called to configure the interceptor with given properties.
Parameters:
configs: Configuration properties map.
Behavior: No operation (noop).
Implementation Details
The class uses a static
ArrayListto accumulate all intercepted consumer records. This makes the captured data accessible from anywhere in the JVM, useful for test assertions or manual inspection.Logging is performed at the trace level for each record's value to avoid noisy logs during normal operation but allow detailed output when trace logging is enabled.
No modifications are made to the records, ensuring the consumer's normal processing flow is not affected.
The interceptor methods for commit, close, and configure are intentionally left as no-ops, reflecting the minimal functionality needed for mocking purposes.
Interaction with the System
This interceptor is designed to be plugged into the Kafka consumer configuration within the Apache Camel Kafka component or testing setups.
When configured, it intercepts all records consumed by Kafka clients, capturing them for debugging or verification.
It does not interfere with the production flow but acts as a passive observer.
Typically used in unit tests or integration tests to verify that messages are consumed as expected without requiring external mocking frameworks.
Visual Diagram
The following class diagram illustrates the structure of `MockConsumerInterceptor` and its relationship to Kafka's `ConsumerInterceptor` interface:
classDiagram
class MockConsumerInterceptor {
+static ArrayList~ConsumerRecords<String,String>~ recordsCaptured
-static final Logger LOG
+ConsumerRecords<String,String> onConsume(ConsumerRecords<String,String> consumerRecords)
+void onCommit(Map<TopicPartition, OffsetAndMetadata> offsets)
+void close()
+void configure(Map<String, ?> configs)
}
MockConsumerInterceptor ..|> ConsumerInterceptor
Summary
`MockConsumerInterceptor.java` provides a simple, effective mock interceptor for Kafka consumers that captures consumed records without altering them. It is useful for testing and debugging Kafka consumer behavior in the Apache Camel ecosystem. By storing all consumed records and logging their content, it allows developers to validate message consumption workflows while maintaining clean separation from production logic.