Class ConcurrentMessageListenerContainer<K,V>
- java.lang.Object
-
- org.springframework.kafka.listener.AbstractMessageListenerContainer<K,V>
-
- org.springframework.kafka.listener.ConcurrentMessageListenerContainer<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.context.ApplicationContextAware
,org.springframework.context.ApplicationEventPublisherAware
,org.springframework.context.Lifecycle
,org.springframework.context.Phased
,org.springframework.context.SmartLifecycle
,GenericMessageListenerContainer<K,V>
,MessageListenerContainer
public class ConcurrentMessageListenerContainer<K,V> extends AbstractMessageListenerContainer<K,V>
Creates 1 or moreKafkaMessageListenerContainer
s based onconcurrency
. If theContainerProperties
is configured withTopicPartition
s, theTopicPartition
s are distributed evenly across the instances.- Author:
- Marius Bogoevici, Gary Russell, Murali Reddy, Jerome Mirc, Artem Bilan, Vladimir Tsanev, Tomaz Fernandes
-
-
Field Summary
-
Fields inherited from class org.springframework.kafka.listener.AbstractMessageListenerContainer
consumerFactory, DEFAULT_PHASE, lifecycleMonitor, logger
-
-
Constructor Summary
Constructors Constructor Description ConcurrentMessageListenerContainer(ConsumerFactory<? super K,? super V> consumerFactory, ContainerProperties containerProperties)
Construct an instance with the supplied configuration properties.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
doStart()
protected void
doStop(java.lang.Runnable callback)
java.util.Collection<org.apache.kafka.common.TopicPartition>
getAssignedPartitions()
Return the assigned topics/partitions for this container.java.util.Map<java.lang.String,java.util.Collection<org.apache.kafka.common.TopicPartition>>
getAssignmentsByClientId()
Return the assigned topics/partitions for this container, by client.id.int
getConcurrency()
java.util.List<KafkaMessageListenerContainer<K,V>>
getContainers()
Return the list ofKafkaMessageListenerContainer
s created by this container.boolean
isChildRunning()
If this container has child containers, return true if at least one child is running.boolean
isContainerPaused()
Return true ifMessageListenerContainer.pause()
has been called; and all consumers in this container have actually paused.boolean
isPartitionPaused(org.apache.kafka.common.TopicPartition topicPartition)
Whether or not this topic's partition is currently paused.java.util.Map<java.lang.String,java.util.Map<org.apache.kafka.common.MetricName,? extends org.apache.kafka.common.Metric>>
metrics()
Return metrics kept by this container's consumer(s), grouped byclient-id
.void
pause()
Pause this container before the next poll().void
pausePartition(org.apache.kafka.common.TopicPartition topicPartition)
Pause this partition before the next poll().void
resume()
Resume this container, if paused, after the next poll().void
resumePartition(org.apache.kafka.common.TopicPartition topicPartition)
Resume this partition, if paused, after the next poll().void
setAlwaysClientIdSuffix(boolean alwaysClientIdSuffix)
Set to false to suppress adding a suffix to the child container's client.id when the concurrency is only 1.void
setConcurrency(int concurrency)
The maximum number of concurrentKafkaMessageListenerContainer
s running.java.lang.String
toString()
-
Methods inherited from class org.springframework.kafka.listener.AbstractMessageListenerContainer
checkGroupId, checkTopics, createSimpleLoggingConsumerRebalanceListener, getAfterRollbackProcessor, getApplicationContext, getApplicationEventPublisher, getBatchInterceptor, getBeanName, getCommonErrorHandler, getContainerProperties, getGenericErrorHandler, getGroupId, getListenerId, getPhase, getRecordInterceptor, isAutoStartup, isInterceptBeforeTx, isPartitionPauseRequested, isPaused, isPauseRequested, isRunning, parentOrThis, publishContainerStoppedEvent, setAfterRollbackProcessor, setApplicationContext, setApplicationEventPublisher, setAutoStartup, setBatchErrorHandler, setBatchInterceptor, setBeanName, setCommonErrorHandler, setErrorHandler, setGenericErrorHandler, setInterceptBeforeTx, setPhase, setRecordInterceptor, setRunning, setTopicCheckTimeout, setupMessageListener, start, stop, stop, stop
-
-
-
-
Constructor Detail
-
ConcurrentMessageListenerContainer
public ConcurrentMessageListenerContainer(ConsumerFactory<? super K,? super V> consumerFactory, ContainerProperties containerProperties)
Construct an instance with the supplied configuration properties. The topic partitions are distributed evenly across the delegateKafkaMessageListenerContainer
s.- Parameters:
consumerFactory
- the consumer factory.containerProperties
- the container properties.
-
-
Method Detail
-
getConcurrency
public int getConcurrency()
-
setConcurrency
public void setConcurrency(int concurrency)
The maximum number of concurrentKafkaMessageListenerContainer
s running. Messages from within the same partition will be processed sequentially.- Parameters:
concurrency
- the concurrency.
-
setAlwaysClientIdSuffix
public void setAlwaysClientIdSuffix(boolean alwaysClientIdSuffix)
Set to false to suppress adding a suffix to the child container's client.id when the concurrency is only 1.- Parameters:
alwaysClientIdSuffix
- false to suppress, true (default) to include.- Since:
- 2.2.14
-
getContainers
public java.util.List<KafkaMessageListenerContainer<K,V>> getContainers()
Return the list ofKafkaMessageListenerContainer
s created by this container.- Returns:
- the list of
KafkaMessageListenerContainer
s created by this container.
-
getAssignedPartitions
public java.util.Collection<org.apache.kafka.common.TopicPartition> getAssignedPartitions()
Description copied from interface:MessageListenerContainer
Return the assigned topics/partitions for this container.- Returns:
- the topics/partitions.
-
getAssignmentsByClientId
public java.util.Map<java.lang.String,java.util.Collection<org.apache.kafka.common.TopicPartition>> getAssignmentsByClientId()
Description copied from interface:MessageListenerContainer
Return the assigned topics/partitions for this container, by client.id.- Returns:
- the topics/partitions.
-
isContainerPaused
public boolean isContainerPaused()
Description copied from interface:MessageListenerContainer
Return true ifMessageListenerContainer.pause()
has been called; and all consumers in this container have actually paused.- Returns:
- true if the container is paused.
-
isChildRunning
public boolean isChildRunning()
Description copied from interface:MessageListenerContainer
If this container has child containers, return true if at least one child is running. If there are not child containers, returnsLifecycle.isRunning()
.- Returns:
- true if a child is running.
-
metrics
public java.util.Map<java.lang.String,java.util.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 byclient-id
.- Returns:
- the consumer(s) metrics grouped by
client-id
- See Also:
Consumer.metrics()
-
doStart
protected void doStart()
- Specified by:
doStart
in classAbstractMessageListenerContainer<K,V>
-
doStop
protected void doStop(java.lang.Runnable callback)
- Specified by:
doStop
in classAbstractMessageListenerContainer<K,V>
-
pause
public void pause()
Description copied from interface:MessageListenerContainer
Pause this container before the next poll(). This is a thread-safe operation, the actual pause is processed by the consumer thread.- Specified by:
pause
in interfaceMessageListenerContainer
- Overrides:
pause
in classAbstractMessageListenerContainer<K,V>
- See Also:
KafkaConsumer.pause(Collection)
-
resume
public void resume()
Description copied from interface:MessageListenerContainer
Resume this container, if paused, after the next poll(). This is a thread-safe operation, the actual resume is processed by the consumer thread.- Specified by:
resume
in interfaceMessageListenerContainer
- Overrides:
resume
in classAbstractMessageListenerContainer<K,V>
- See Also:
KafkaConsumer.resume(Collection)
-
pausePartition
public void pausePartition(org.apache.kafka.common.TopicPartition topicPartition)
Description copied from interface:MessageListenerContainer
Pause this partition before the next poll(). This is a thread-safe operation, the actual pause is processed by the consumer thread.- Specified by:
pausePartition
in interfaceMessageListenerContainer
- Overrides:
pausePartition
in classAbstractMessageListenerContainer<K,V>
- Parameters:
topicPartition
- the topicPartition to pause.
-
resumePartition
public void resumePartition(org.apache.kafka.common.TopicPartition topicPartition)
Description copied from interface:MessageListenerContainer
Resume this partition, if paused, after the next poll(). This is a thread-safe operation, the actual pause is processed by the consumer thread.- Specified by:
resumePartition
in interfaceMessageListenerContainer
- Overrides:
resumePartition
in classAbstractMessageListenerContainer<K,V>
- Parameters:
topicPartition
- the topicPartition to resume.
-
isPartitionPaused
public boolean isPartitionPaused(org.apache.kafka.common.TopicPartition topicPartition)
Description copied from interface:MessageListenerContainer
Whether or not this topic's partition is currently paused.- Parameters:
topicPartition
- the topic partition to check- Returns:
- true if this partition has been paused.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-