Class ShareKafkaListenerContainerFactory<K,V>
- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationEventPublisherAware, KafkaListenerContainerFactory<ShareKafkaMessageListenerContainer<K,V>>
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 Summary
ConstructorsConstructorDescriptionShareKafkaListenerContainerFactory(ShareConsumerFactory<K, V> shareConsumerFactory) Construct an instance with the provided consumer factory. -
Method Summary
Modifier and TypeMethodDescriptioncreateContainer(String... topics) Create and configure a container without a listener; used to create containers that are not used for KafkaListener annotations.createContainer(Pattern topicPattern) Create and configure a container without a listener; used to create containers that are not used for KafkaListener annotations.createContainer(TopicPartitionOffset... topicPartitions) Create and configure a container without a listener; used to create containers that are not used for KafkaListener annotations.protected ShareKafkaMessageListenerContainer<K, V> createContainerInstance(KafkaListenerEndpoint endpoint) Create a container instance for the provided endpoint.createListenerContainer(KafkaListenerEndpoint endpoint) Create aMessageListenerContainerfor the givenKafkaListenerEndpoint.Obtain the factory-level container properties - set properties as needed and they will be copied to each listener container instance created by this factory.protected voidinitializeContainer(ShareKafkaMessageListenerContainer<K, V> instance, KafkaListenerEndpoint endpoint) Initialize the provided container with common configuration properties.voidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext) voidsetApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) voidsetAutoStartup(boolean autoStartup) Set whether containers created by this factory should auto-start.voidsetConcurrency(int concurrency) Set the concurrency for containers created by this factory.voidsetPhase(int phase) Set the phase in which containers created by this factory should start and stop.
-
Constructor Details
-
Method Details
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) - Specified by:
setApplicationContextin interfaceorg.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
ShareConsumerinstance 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
concurrencyattribute on@KafkaListener.- Parameters:
concurrency- the number of consumer threads (must be greater than 0)
-
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:
setApplicationEventPublisherin interfaceorg.springframework.context.ApplicationEventPublisherAware
-
createListenerContainer
public ShareKafkaMessageListenerContainer<K,V> createListenerContainer(KafkaListenerEndpoint endpoint) Description copied from interface:KafkaListenerContainerFactoryCreate aMessageListenerContainerfor the givenKafkaListenerEndpoint. Containers created using this method are added to the listener endpoint registry.- Specified by:
createListenerContainerin interfaceKafkaListenerContainerFactory<K>- Parameters:
endpoint- the endpoint to configure- Returns:
- the created container
-
createContainer
public ShareKafkaMessageListenerContainer<K,V> createContainer(TopicPartitionOffset... topicPartitions) Description copied from interface:KafkaListenerContainerFactoryCreate 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:
createContainerin interfaceKafkaListenerContainerFactory<K>- Parameters:
topicPartitions- the topicPartitions to assign.- Returns:
- the container.
-
createContainer
Description copied from interface:KafkaListenerContainerFactoryCreate 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:
createContainerin interfaceKafkaListenerContainerFactory<K>- Parameters:
topics- the topics.- Returns:
- the container.
-
createContainer
Description copied from interface:KafkaListenerContainerFactoryCreate 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:
createContainerin interfaceKafkaListenerContainerFactory<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
-