This version is still in development and is not considered stable yet. For the latest stable version, please use spring-cloud-stream 4.1.4!spring-doc.cn

Kafka Binder Listener Container Customizers

Spring Cloud Stream provides powerful customization options for message listener containers through the use of customizers. This section covers the customizer interfaces available for Kafka: ListenerContainerCustomizer, its Kafka-specific extension KafkaListenerContainerCustomizer, and the specialized ListenerContainerWithDlqAndRetryCustomizer.spring-doc.cn

ListenerContainerCustomizer

The ListenerContainerCustomizer is a generic interface in Spring Cloud Stream that allows customization of message listener containers.spring-doc.cn

Purpose

Use this customizer when you need to modify the behavior of the listener container.spring-doc.cn

Usage

To use the ListenerContainerCustomizer, create a bean that implements this interface in your configuration:spring-doc.cn

@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> genericCustomizer() {
    return (container, destinationName, group) -> {
        // Customize the container here
    };
}

The ListenerContainerCustomizer interface defines the following method:spring-doc.cn

void configure(C container, String destinationName, String group);

KafkaListenerContainerCustomizer

The KafkaListenerContainerCustomizer interface extends ListenerContainerCustomizer to modify the behavior of the listener container and provides access to the binding-specific extended Kafka consumer properties.spring-doc.cn

Purpose

Use this customizer when you need to access the binding-specific extended Kafka consumer properties while customizing the listener container.spring-doc.cn

Usage

To use the KafkaListenerContainerCustomizer, create a bean that implements this interface in your configuration:spring-doc.cn

@Bean
public KafkaListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> kafkaCustomizer() {
    return (container, destinationName, group, properties) -> {
        // Customize the Kafka container here
    };
}

The KafkaListenerContainerCustomizer interface adds the following method:spring-doc.cn

default void configureKafkaListenerContainer(
    C container,
    String destinationName,
    String group,
    ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties) {
        configure(container, destinationName, group);
}

This method extends the base configure method with an additional parameter:spring-doc.cn

  • extendedConsumerProperties: Extended consumer properties, including Kafka-specific properties.spring-doc.cn

ListenerContainerWithDlqAndRetryCustomizer

The ListenerContainerWithDlqAndRetryCustomizer interface provides additional customization options for scenarios involving Dead Letter Queues (DLQ) and retry mechanisms.spring-doc.cn

Purpose

Use this customizer when you need to fine-tune DLQ behavior or implement custom retry logic for your Kafka consumers.spring-doc.cn

Usage

To use the ListenerContainerWithDlqAndRetryCustomizer, create a bean that implements this interface in your configuration:spring-doc.cn

@Bean
public ListenerContainerWithDlqAndRetryCustomizer dlqCustomizer() {
    return (container, destinationName, group, dlqDestinationResolver, backOff, properties) -> {
        // Access the container here with access to the extended consumer binding properties.
    };
}

The ListenerContainerWithDlqAndRetryCustomizer interface defines the following method:spring-doc.cn

void configure(
    AbstractMessageListenerContainer<?, ?> container,
    String destinationName,
    String group,
    BiFunction<ConsumerRecord<?, ?>, Exception, TopicPartition> dlqDestinationResolver,
    BackOff backOff,
    ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties
);
  • container: The Kafka listener container to customize.spring-doc.cn

  • destinationName: The name of the destination (topic).spring-doc.cn

  • group: The consumer group ID.spring-doc.cn

  • dlqDestinationResolver: A function to resolve the DLQ destination for a failed record.spring-doc.cn

  • backOff: The backoff policy for retries.spring-doc.cn

  • extendedConsumerProperties: Extended consumer properties, including Kafka-specific properties.spring-doc.cn

Summary

  • ListenerContainerWithDlqAndRetryCustomizer is used if DLQ is enabled.spring-doc.cn

  • KafkaListenerContainerCustomizer is used for Kafka-specific customization without DLQ.spring-doc.cn

  • The base ListenerContainerCustomizer is used for generic customization.spring-doc.cn

This hierarchical approach allows for flexible and specific customization of your Kafka listener containers in Spring Cloud Stream applications.spring-doc.cn