Class ShareKafkaListenerContainerFactory<K,V>

java.lang.Object
org.springframework.kafka.config.ShareKafkaListenerContainerFactory<K,V>
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationEventPublisherAware, KafkaListenerContainerFactory<ShareKafkaMessageListenerContainer<K,V>>

public class ShareKafkaListenerContainerFactory<K,V> extends Object implements KafkaListenerContainerFactory<ShareKafkaMessageListenerContainer<K,V>>, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.ApplicationContextAware
A KafkaListenerContainerFactory implementation to create ShareKafkaMessageListenerContainer instances for Kafka's share consumer model.

This factory provides common configuration and lifecycle management for share consumer containers. It handles the creation of containers based on endpoints, topics, or patterns, and applies common configuration properties to the created containers.

The share consumer model enables cooperative rebalancing, allowing consumers to maintain ownership of some partitions while relinquishing others during rebalances, which can reduce disruption compared to the classic consumer model.

Since:
4.0
  • Constructor Details

    • ShareKafkaListenerContainerFactory

      public ShareKafkaListenerContainerFactory(ShareConsumerFactory<K,V> shareConsumerFactory)
      Construct an instance with the provided consumer factory.
      Parameters:
      shareConsumerFactory - the share consumer factory
  • Method Details

    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
    • setAutoStartup

      public void setAutoStartup(boolean autoStartup)
      Set whether containers created by this factory should auto-start.
      Parameters:
      autoStartup - true to auto-start
    • setPhase

      public void setPhase(int phase)
      Set the phase in which containers created by this factory should start and stop.
      Parameters:
      phase - the phase
    • setConcurrency

      public void setConcurrency(int concurrency)
      Set the concurrency for containers created by this factory.

      This specifies the number of consumer threads to create within each container. Each thread creates its own ShareConsumer instance and participates in the same share group. The Kafka broker distributes records across all consumer instances, providing record-level load balancing.

      This can be overridden per listener endpoint using the concurrency attribute on @KafkaListener.

      Parameters:
      concurrency - the number of consumer threads (must be greater than 0)
    • getContainerProperties

      public ContainerProperties getContainerProperties()
      Obtain the factory-level container properties - set properties as needed and they will be copied to each listener container instance created by this factory.
      Returns:
      the properties.
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
      Specified by:
      setApplicationEventPublisher in interface org.springframework.context.ApplicationEventPublisherAware
    • createListenerContainer

      public ShareKafkaMessageListenerContainer<K,V> createListenerContainer(KafkaListenerEndpoint endpoint)
      Description copied from interface: KafkaListenerContainerFactory
      Create a MessageListenerContainer for the given KafkaListenerEndpoint. Containers created using this method are added to the listener endpoint registry.
      Specified by:
      createListenerContainer in interface KafkaListenerContainerFactory<K>
      Parameters:
      endpoint - the endpoint to configure
      Returns:
      the created container
    • initializeContainer

      protected void initializeContainer(ShareKafkaMessageListenerContainer<K,V> instance, KafkaListenerEndpoint endpoint)
      Initialize the provided container with common configuration properties.
      Parameters:
      instance - the container instance
      endpoint - the endpoint
    • createContainer

      public ShareKafkaMessageListenerContainer<K,V> createContainer(TopicPartitionOffset... topicPartitions)
      Description copied from interface: KafkaListenerContainerFactory
      Create and configure a container without a listener; used to create containers that are not used for KafkaListener annotations. Containers created using this method are not added to the listener endpoint registry.
      Specified by:
      createContainer in interface KafkaListenerContainerFactory<K>
      Parameters:
      topicPartitions - the topicPartitions to assign.
      Returns:
      the container.
    • createContainer

      public ShareKafkaMessageListenerContainer<K,V> createContainer(String... topics)
      Description copied from interface: KafkaListenerContainerFactory
      Create and configure a container without a listener; used to create containers that are not used for KafkaListener annotations. Containers created using this method are not added to the listener endpoint registry.
      Specified by:
      createContainer in interface KafkaListenerContainerFactory<K>
      Parameters:
      topics - the topics.
      Returns:
      the container.
    • createContainer

      public ShareKafkaMessageListenerContainer<K,V> createContainer(Pattern topicPattern)
      Description copied from interface: KafkaListenerContainerFactory
      Create and configure a container without a listener; used to create containers that are not used for KafkaListener annotations. Containers created using this method are not added to the listener endpoint registry.
      Specified by:
      createContainer in interface KafkaListenerContainerFactory<K>
      Parameters:
      topicPattern - the topicPattern.
      Returns:
      the container.
    • createContainerInstance

      protected ShareKafkaMessageListenerContainer<K,V> createContainerInstance(KafkaListenerEndpoint endpoint)
      Create a container instance for the provided endpoint.
      Parameters:
      endpoint - the endpoint
      Returns:
      the container instance