TaskHealthState.java

Overview

The `TaskHealthState` class encapsulates the health and status information of a Kafka consumer task within the Apache Camel Kafka component. It provides a thread-safe, immutable snapshot representing whether the Kafka consumer is ready, terminated, or recovering, along with diagnostic details such as the last error encountered, client identification, connection properties, and backoff intervals during recovery attempts.

This class is designed to be used internally by health checking mechanisms without exposing sensitive or mutable internal state. By encapsulating these details, it aids in monitoring and managing the lifecycle and health status of Kafka consumer tasks in a robust and thread-safe manner.


Class: TaskHealthState

Purpose

Represents the immutable health state of a Kafka consumer task, including readiness, termination status, recoverability, error diagnostics, and client configuration details.

Package

package org.apache.camel.component.kafka;

Properties (Fields)

Field

Type

Description

`ready`

`boolean`

Indicates if the Kafka consumer task is currently ready to process messages.

`isTerminated`

`boolean`

True if the consumer task has terminated and given up recovery attempts.

`isRecoverable`

`boolean`

True if the consumer task is in a recoverable state and retrying recovery.

`lastError`

`Exception`

The most recent exception encountered by the consumer task, if any.

`clientId`

`String`

Identifier for the Kafka consumer client instance.

`bootstrapServers`

`String`

Kafka bootstrap servers used by the consumer, derived from client properties.

`currentBackoffInterval`

`long`

Current backoff interval in milliseconds used during recovery retries.

`clientProperties`

`Properties`

Kafka consumer client configuration properties.


Constructor

public TaskHealthState(
    boolean ready,
    boolean isTerminated,
    boolean isRecoverable,
    Exception lastError,
    String clientId,
    long currentBackoffInterval,
    Properties clientProperties
)

Public Methods

boolean isReady()

boolean isTerminated()

boolean isRecoverable()

Exception getLastError()

String getClientId()

long getCurrentBackoffInterval()

String getBootstrapServers()

String getGroupId()

String buildStateMessage()


Private Methods

Throwable extractRootCause(Throwable throwable)


Implementation Details and Algorithms


Interaction with Other System Components


Usage Example

Properties props = new Properties();
props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka-broker1:9092");
props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "my-group");

TaskHealthState healthState = new TaskHealthState(
    false,            // ready
    false,            // isTerminated
    true,             // isRecoverable
    new Exception("Timeout Exception"), // lastError
    "client-123",
    30000,            // 30 seconds backoff
    props
);

System.out.println(healthState.buildStateMessage());
// Output might be:
// "KafkaConsumer is not ready (recovery in progress using 30 seconds intervals). - Error: Timeout Exception"

Class Diagram

classDiagram
    class TaskHealthState {
        -boolean ready
        -boolean isTerminated
        -boolean isRecoverable
        -Exception lastError
        -String clientId
        -String bootstrapServers
        -long currentBackoffInterval
        -Properties clientProperties
        +TaskHealthState(boolean, boolean, boolean, Exception, String, long, Properties)
        +boolean isReady()
        +boolean isTerminated()
        +boolean isRecoverable()
        +Exception getLastError()
        +String getClientId()
        +long getCurrentBackoffInterval()
        +String getBootstrapServers()
        +String getGroupId()
        +String buildStateMessage()
        -Throwable extractRootCause(Throwable)
    }

Summary

`TaskHealthState.java` provides a clean, immutable representation of the Kafka consumer task's health within the Apache Camel Kafka component. It encapsulates key status flags, error details, client configuration info, and recovery state, enabling transparent and thread-safe health monitoring. This class plays a crucial role in integration with health checks and operational monitoring, facilitating robust Kafka consumer lifecycle management.