KafkaBreakOnFirstErrorWithBatchUsingSyncCommitManagerIT.java


Overview

`KafkaBreakOnFirstErrorWithBatchUsingSyncCommitManagerIT` is an integration test class designed to verify the behavior of the Apache Camel Kafka component when using the **breakOnFirstError** option combined with **manual offset commit** managed via a synchronous commit manager.

The test validates that when a message processing failure occurs (specifically on a designated payload), Camel breaks processing on the first error encountered within a batch of Kafka records, and retries the failed message indefinitely without committing its offset. This ensures no subsequent messages in the batch are processed until the error is resolved (simulated here by continuous failure). The test also confirms that manual offset commits are controlled correctly by Camel's [DefaultKafkaManualCommitFactory](/projects/289/68562).

This class extends `BaseKafkaTestSupport`, leveraging its Kafka test infrastructure to produce messages and manage Kafka topics.


Detailed Explanation

Package and Imports


Class: KafkaBreakOnFirstErrorWithBatchUsingSyncCommitManagerIT

Description

This class tests the "break on first error" functionality in Kafka consumers with batch processing and manual commits using a synchronous commit manager.

Constants

Name

Description

`ROUTE_ID`

String identifier for the route used in the test.

`TOPIC`

Kafka topic name used in the test.

Logger

Fields

Field

Type

Description

`errorPayloads`

`CopyOnWriteArrayList`

Thread-safe list capturing payloads that trigger errors during processing.

`to`

`MockEndpoint` (injected)

Apache Camel mock endpoint to assert consumed messages in the route.

`producer`

KafkaProducer

Kafka producer instance used to send test messages to the Kafka topic.


Lifecycle Methods

before()

after()


Test Method

kafkaBreakOnFirstErrorBasicCapability()


Route Definition

Method: createRouteBuilder()


Helper Methods

publishMessagesToKafka()

ifIsPayloadWithErrorThrowException(Exchange exchange)


Important Implementation Details


Interaction with Other Parts of the System


Usage Example

This test class is primarily for automated testing and is run as part of the integration test suite. It can be executed using standard JUnit 5 test runners or build tools like Maven or Gradle.


Mermaid Class Diagram

classDiagram
    class KafkaBreakOnFirstErrorWithBatchUsingSyncCommitManagerIT {
        -static final String ROUTE_ID
        -static final String TOPIC
        -static final Logger LOG
        -List~String~ errorPayloads
        -MockEndpoint to
        -KafkaProducer~String, String~ producer
        +void before()
        +void after()
        +void kafkaBreakOnFirstErrorBasicCapability()
        +RouteBuilder createRouteBuilder()
        -void publishMessagesToKafka()
        -void ifIsPayloadWithErrorThrowException(Exchange)
    }
    KafkaBreakOnFirstErrorWithBatchUsingSyncCommitManagerIT --|> BaseKafkaTestSupport

Summary

`KafkaBreakOnFirstErrorWithBatchUsingSyncCommitManagerIT` is a specialized integration test validating that Apache Camel's Kafka component correctly handles batch consumption with manual offset commits, halting and retrying on the first error within a batch. It ensures offsets are committed only for successfully processed messages and that error messages are retried indefinitely when [breakOnFirstError](/projects/289/68714) is enabled. The test uses a combination of Camel routes, Kafka producer/consumer settings, and JUnit/Awaitility assertions to simulate and verify this behavior.