ProcessingResult.java
Overview
`ProcessingResult.java` defines the `ProcessingResult` class, a simple immutable data holder used within the Apache Camel Kafka component to represent the outcome of processing one or more Kafka consumer records. It encapsulates key information about the processing status, including whether processing failed, whether a "break on error" condition was triggered, and metadata about the Kafka topic, partition, and offset associated with the processed records.
This class primarily serves as a communication object within the Kafka consumer processing workflow, enabling other components to react accordingly based on success or failure states and consumer position tracking.
Class: ProcessingResult
Purpose
`ProcessingResult` captures the result of processing messages consumed from Kafka topics. It provides:
Flags indicating if processing failed and if a configured "break on error" policy was hit.
Kafka metadata: topic name, partition number, and offset of the processed record(s).
A ready-to-use singleton instance representing an unprocessed state.
Properties
Property | Type | Description |
|---|---|---|
`breakOnErrorHit` | boolean | Indicates if processing stopped due to a break-on-error condition. |
`failed` | boolean | Indicates if processing of the message(s) failed. |
`topic` | String | Kafka topic name associated with the processed record. May be `null` if unspecified. |
`partition` | int | Partition number of the Kafka topic. |
`offset` | long | Offset of the Kafka message in the partition. |
Constructors
ProcessingResult(boolean breakOnErrorHit, boolean failed)
Creates a `ProcessingResult` instance without specifying topic, partition, or offset.
Parameters:
breakOnErrorHit:trueif the processing hit a break-on-error condition.failed:trueif processing failed.
Usage example:
ProcessingResult result = new ProcessingResult(true, false);
ProcessingResult(boolean breakOnErrorHit, boolean failed, String topic, int partition, long offset)
Creates a `ProcessingResult` instance with detailed Kafka record metadata.
Parameters:
breakOnErrorHit:trueif the processing hit a break-on-error condition.failed:trueif processing failed.topic: Kafka topic name.partition: Partition number within the topic.offset: Offset in the partition.
Usage example:
ProcessingResult detailedResult = new ProcessingResult(false, true, "my-topic", 2, 12345L);
Methods
Method | Return Type | Description |
|---|---|---|
`isBreakOnErrorHit()` | boolean | Returns whether the processing hit a break-on-error condition. |
`isFailed()` | boolean | Returns whether the processing failed. |
`getTopic()` | String | Returns the Kafka topic name associated with this result. |
`getPartition()` | int | Returns the partition number of the Kafka topic. |
`getOffset()` | long | Returns the offset of the consumer record in the partition. |
`static newUnprocessed()` | ProcessingResult | Returns a singleton instance representing an unprocessed result (`breakOnErrorHit=false`, `failed=false`, no topic info). |
Usage Example
// Create a processing result indicating failure and break on error
ProcessingResult failureResult = new ProcessingResult(true, true, "orders-topic", 1, 1024L);
if (failureResult.isFailed()) {
// Handle failure logic here
System.out.println("Processing failed for topic: " + failureResult.getTopic());
}
// Obtain an unprocessed result singleton
ProcessingResult unprocessed = ProcessingResult.newUnprocessed();
Implementation Details
The class is declared
finalto prevent subclassing, ensuring immutability and thread safety.All fields are
finaland private, set only via constructors.A static singleton instance
UNPROCESSED_RESULTrepresents the state of "no processing done" — useful as a default or initial state.No setters are provided, enforcing immutability.
The class does not implement interfaces such as
SerializableorComparable, indicating it is intended solely for internal data transport rather than serialization or sorting.The class lives in the package
org.apache.camel.component.kafka.consumer.support, indicating its role as a support/helper class in Kafka consumer processing within Apache Camel.
Interaction with Other Components
This class is used within the Kafka consumer component of Apache Camel to represent the outcome after processing one or more Kafka
ConsumerRecords.It likely interacts with the Kafka consumer loop, error handling mechanisms, and offset committing logic.
Downstream components can inspect
ProcessingResultinstances to decide whether to continue processing, stop on error, or commit offsets.The presence of topic, partition, and offset data facilitates tracking the exact Kafka position where errors or breaks occur, enabling precise recovery or logging.
Visual Diagram: Class Structure
classDiagram
class ProcessingResult {
-boolean breakOnErrorHit
-boolean failed
-String topic
-int partition
-long offset
+ProcessingResult(breakOnErrorHit: boolean, failed: boolean)
+ProcessingResult(breakOnErrorHit: boolean, failed: boolean, topic: String, partition: int, offset: long)
+isBreakOnErrorHit(): boolean
+isFailed(): boolean
+getTopic(): String
+getPartition(): int
+getOffset(): long
+static newUnprocessed(): ProcessingResult
}
Summary
`ProcessingResult.java` is a concise, immutable data carrier class that encapsulates processing outcomes for Kafka consumer records in Apache Camel. It provides essential flags and Kafka metadata that enable error handling, flow control, and offset management during Kafka message consumption. Its design prioritizes simplicity, thread safety, and ease of use in the broader Kafka processing workflow.