Class ShareKafkaMessageListenerContainer<K,V>
- Type Parameters:
K- the key typeV- 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
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
@KafkaListenerannotation - 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:
-
Field Summary
Fields inherited from class AbstractShareKafkaMessageListenerContainer
DEFAULT_PHASE, lifecycleLock, logger, shareConsumerFactory -
Constructor Summary
ConstructorsConstructorDescriptionShareKafkaMessageListenerContainer(ShareConsumerFactory<? super K, ? super V> shareConsumerFactory, ContainerProperties containerProperties) Construct an instance with the supplied configuration properties. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoStart()protected voiddoStop()@Nullable StringGet theclient.idfor the consumer.booleanReturn true if the container is running, has never been started, or has been stopped.metrics()Return metrics kept by this container's consumer(s), grouped byclient-id.voidsetClientId(String clientId) Set theclient.idto use for the consumer.voidsetConcurrency(int concurrency) Set the level of concurrency.Methods inherited from class AbstractShareKafkaMessageListenerContainer
destroy, getApplicationContext, getApplicationEventPublisher, getBeanName, getContainerProperties, getGroupId, getListenerId, getPhase, isAutoStartup, isRunning, setApplicationContext, setApplicationEventPublisher, setAutoStartup, setBeanName, setPhase, setRunning, setupMessageListener, start, stop, stopMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface MessageListenerContainer
childStarted, childStopped, enforceRebalance, getAssignedPartitions, getAssignmentsByClientId, getContainerFor, getListenerInfo, getMainListenerId, isChildRunning, isContainerPaused, isPartitionPaused, isPartitionPauseRequested, isPauseRequested, pause, pausePartition, resume, resumePartition, stopAbnormallyMethods inherited from interface org.springframework.context.SmartLifecycle
isPauseable
-
Constructor Details
-
Method Details
-
getClientId
Get theclient.idfor the consumer.- Returns:
- the client id, or null if not set
-
setClientId
Set theclient.idto 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 ownShareConsumerinstance. 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:MessageListenerContainerReturn 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:MessageListenerContainerReturn metrics kept by this container's consumer(s), grouped byclient-id.- Returns:
- the consumer(s) metrics grouped by
client-id - See Also:
-
doStart
protected void doStart()- Specified by:
doStartin classAbstractShareKafkaMessageListenerContainer<K,V>
-
doStop
protected void doStop()- Specified by:
doStopin classAbstractShareKafkaMessageListenerContainer<K,V>
-