KafkaBreakOnFirstErrorWithBatchUsingAsyncCommitManagerIT.java


Overview

`KafkaBreakOnFirstErrorWithBatchUsingAsyncCommitManagerIT.java` is an integration test class within the Apache Camel Kafka component module. Its primary purpose is to **verify the behavior of Kafka consumer routes configured with the `breakOnFirstError` option enabled, using asynchronous manual offset commit management in batch processing mode**.

This test ensures that when a message processing error occurs, the route properly breaks processing on the first error and repeatedly retries the failing message without committing its offset. This behavior prevents subsequent messages from being consumed until the error is resolved, a key feature for reliable message processing in Kafka.

The class leverages:


Class: KafkaBreakOnFirstErrorWithBatchUsingAsyncCommitManagerIT

Description

Integration test class extending `BaseKafkaTestSupport` to validate Kafka consumer behavior with batch consumption and error handling using async commit manager in Camel.

Key Constants

Constant

Description

`ROUTE_ID`

The fixed route identifier string `"breakOnFirstErrorBatchIT"` used to manage the lifecycle of the Camel route.

`TOPIC`

Unique Kafka topic name generated by appending a random UUID to `"breakOnFirstErrorBatchIT"`. Ensures isolated test data.

Logger

Fields

Field

Type

Description

`errorPayloads`

`List`

Thread-safe list collecting payloads of messages that triggered errors during processing.

`to`

`MockEndpoint`

Injected mock endpoint to which the Camel route sends successfully processed messages, used for assertions.

`producer`

`KafkaProducer`

Kafka producer instance used to send test messages to the Kafka topic before starting the consumer route.


Lifecycle Methods

before()

after()


Test Methods

kafkaBreakOnFirstErrorBasicCapability()


Route Configuration

Method: createRouteBuilder()

kafka:<TOPIC>?groupId=<ROUTE_ID>&autoOffsetReset=earliest&autoCommitEnable=false&allowManualCommit=true&breakOnFirstError=true&maxPollRecords=3&pollTimeoutMs=1000&keyDeserializer=org.apache.kafka.common.serialization.StringDeserializer&valueDeserializer=org.apache.kafka.common.serialization.StringDeserializer&kafkaManualCommitFactory=#class:org.apache.camel.component.kafka.consumer.DefaultKafkaManualAsyncCommitFactory&interceptorClasses=org.apache.camel.component.kafka.MockConsumerInterceptor

Helper Methods

publishMessagesToKafka()


ifIsPayloadWithErrorThrowException(Exchange exchange)


Important Implementation Details


Interactions with Other Components


Usage Example

This integration test is not designed for direct reuse but as a template illustrating how to:


Visual Diagram

classDiagram
    class KafkaBreakOnFirstErrorWithBatchUsingAsyncCommitManagerIT {
        - List<String> errorPayloads
        - MockEndpoint to
        - KafkaProducer<String, String> producer
        + void before()
        + void after()
        + void kafkaBreakOnFirstErrorBasicCapability()
        + RouteBuilder createRouteBuilder()
        - void publishMessagesToKafka()
        - void ifIsPayloadWithErrorThrowException(Exchange)
    }

    KafkaBreakOnFirstErrorWithBatchUsingAsyncCommitManagerIT --|> BaseKafkaTestSupport

    class RouteBuilder {
        + void configure()
    }

    KafkaBreakOnFirstErrorWithBatchUsingAsyncCommitManagerIT o-- RouteBuilder : creates >

Summary

`KafkaBreakOnFirstErrorWithBatchUsingAsyncCommitManagerIT.java` is a focused integration test validating Apache Camel Kafka consumer behavior under batch processing with manual asynchronous commits and strict error handling. It demonstrates how Camel can be configured to halt consumer progress on the first error and retry problematic messages indefinitely, ensuring robust and reliable Kafka message processing in production systems.