Class ShareKafkaMessageListenerContainer<K,V>

java.lang.Object
org.springframework.kafka.listener.AbstractShareKafkaMessageListenerContainer<K,V>
org.springframework.kafka.listener.ShareKafkaMessageListenerContainer<K,V>
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle, GenericMessageListenerContainer<K,V>, MessageListenerContainer

public class ShareKafkaMessageListenerContainer<K,V> extends AbstractShareKafkaMessageListenerContainer<K,V>
Share consumer container using the Java ShareConsumer.

This container provides support for Kafka share groups, enabling cooperative consumption where multiple consumers can process records from the same partitions. Unlike traditional consumer groups with exclusive partition assignment, share groups allow load balancing at the record level.

Concurrency Support:

This container supports running multiple consumer threads via the setConcurrency(int) method. Each thread creates its own ShareConsumer instance and polls independently. Unlike traditional consumer groups where concurrency involves partition distribution, share consumers leverage Kafka's record-level distribution across all group members. This means multiple threads in the same container participate in the same share group, with the broker distributing records across all consumer instances.

Key features:

  • Explicit and implicit acknowledgment modes
  • Automatic error handling with REJECT acknowledgments
  • Poll-level acknowledgment constraints in explicit mode
  • Integration with Spring's @KafkaListener annotation
  • Configurable concurrency for increased throughput

Acknowledgment Modes:

  • Implicit: Records are automatically acknowledged as ACCEPT after successful processing or REJECT on errors
  • Explicit: Application must manually acknowledge each record; subsequent polls are blocked until all records from the previous poll are acknowledged
Since:
4.0
See Also:
  • Constructor Details

    • ShareKafkaMessageListenerContainer

      public ShareKafkaMessageListenerContainer(ShareConsumerFactory<? super K, ? super V> shareConsumerFactory, ContainerProperties containerProperties)
      Construct an instance with the supplied configuration properties.
      Parameters:
      shareConsumerFactory - the share consumer factory
      containerProperties - the container properties
  • Method Details

    • getClientId

      public @Nullable String getClientId()
      Get the client.id for the consumer.
      Returns:
      the client id, or null if not set
    • setClientId

      public void setClientId(String clientId)
      Set the client.id to use for the consumer.
      Parameters:
      clientId - the client id to set
    • setConcurrency

      public void setConcurrency(int concurrency)
      Set the level of concurrency. This will create the specified number of consumer threads, each with its own ShareConsumer instance. All consumers participate in the same share group, leveraging Kafka's record-level distribution for load balancing.

      Must be called before the container is started.

      Parameters:
      concurrency - the concurrency level (must be greater than 0)
    • isInExpectedState

      public boolean isInExpectedState()
      Description copied from interface: MessageListenerContainer
      Return true if the container is running, has never been started, or has been stopped.
      Returns:
      true if the state is as expected.
      See Also:
    • metrics

      public Map<String, Map<org.apache.kafka.common.MetricName, ? extends org.apache.kafka.common.Metric>> metrics()
      Description copied from interface: MessageListenerContainer
      Return metrics kept by this container's consumer(s), grouped by client-id.
      Returns:
      the consumer(s) metrics grouped by client-id
      See Also:
      • Consumer.metrics()
    • doStart

      protected void doStart()
      Specified by:
      doStart in class AbstractShareKafkaMessageListenerContainer<K,V>
    • doStop

      protected void doStop()
      Specified by:
      doStop in class AbstractShareKafkaMessageListenerContainer<K,V>