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:

Acknowledgment Modes:

  • EXPLICIT (default): Container-managed. The container sends ACCEPT after successful processing and delegates error handling to the ShareConsumerRecordRecoverer (default: REJECT). Equivalent to disabling auto.commit on a regular consumer.
  • MANUAL: Listener-managed. The listener must acknowledge each record via the provided ShareAcknowledgment. Subsequent polls are blocked until all records from the previous poll are acknowledged.
  • IMPLICIT: Kafka client implicit mode. The broker auto-accepts all records regardless of processing outcome. No per-record acknowledgment control is available.
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()
      Return false if a consumer thread exited due to an exception or error while the container was running, or if the container was stopped with stopAbnormally(Runnable); the state is reset when the container is started again.
      Returns:
      true if the state is as expected.
      See Also:
    • stopAbnormally

      public void stopAbnormally(Runnable callback)
      Description copied from interface: MessageListenerContainer
      Stop the container after some exception so that MessageListenerContainer.isInExpectedState() will return false.
      Parameters:
      callback - the callback.
      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()
    • getClientInstanceIds

      public Map<String, org.apache.kafka.common.Uuid> getClientInstanceIds(Duration timeout)
      Get the broker-assigned client instance IDs, keyed by the client id of each running share consumer. The instance id correlates client metrics with broker-side telemetry and is distinct from the client id returned by getClientId(). Returns an empty map when the container is not running.

      The client instance id is requested from each consumer sequentially, so the worst-case wait is concurrency x timeout. Client telemetry must be enabled (enable.metrics.push=true, the default).

      Parameters:
      timeout - the maximum time to wait per consumer for the broker to assign an instance id (must be non-negative; zero means do not wait)
      Returns:
      the client instance ids keyed by client id; the value is null for a consumer whose id was not assigned within the timeout
      Since:
      4.2
      See Also:
    • doStart

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

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