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

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)

// 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)

void close()

void configure(Map<String, ?> configs)


Implementation Details


Interaction with the System


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.