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.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 ConcurrentMessageListenerContainer<K,V> extends AbstractMessageListenerContainer<K,V>
Creates 1 or moreKafkaMessageListenerContainers based onconcurrency. If theContainerPropertiesis configured withTopicPartitions, theTopicPartitions 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 voiddoStart()protected voiddoStop(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.intgetConcurrency()java.util.List<KafkaMessageListenerContainer<K,V>>getContainers()Return the list ofKafkaMessageListenerContainers created by this container.booleanisChildRunning()If this container has child containers, return true if at least one child is running.booleanisContainerPaused()Return true ifMessageListenerContainer.pause()has been called; and all consumers in this container have actually paused.booleanisPartitionPaused(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.voidpause()Pause this container before the next poll().voidpausePartition(org.apache.kafka.common.TopicPartition topicPartition)Pause this partition before the next poll().voidresume()Resume this container, if paused, after the next poll().voidresumePartition(org.apache.kafka.common.TopicPartition topicPartition)Resume this partition, if paused, after the next poll().voidsetAlwaysClientIdSuffix(boolean alwaysClientIdSuffix)Set to false to suppress adding a suffix to the child container's client.id when the concurrency is only 1.voidsetConcurrency(int concurrency)The maximum number of concurrentKafkaMessageListenerContainers running.java.lang.StringtoString()-
Methods inherited from class org.springframework.kafka.listener.AbstractMessageListenerContainer
checkGroupId, checkTopics, createSimpleLoggingConsumerRebalanceListener, getAfterRollbackProcessor, getApplicationContext, getApplicationEventPublisher, getBatchInterceptor, getBeanName, getContainerProperties, getGenericErrorHandler, getGroupId, getListenerId, getPhase, getRecordInterceptor, isAutoStartup, isInterceptBeforeTx, isPartitionPauseRequested, isPaused, isPauseRequested, isRunning, parentOrThis, publishContainerStoppedEvent, setAfterRollbackProcessor, setApplicationContext, setApplicationEventPublisher, setAutoStartup, setBatchErrorHandler, setBatchInterceptor, setBeanName, setErrorHandler, setGenericErrorHandler, setInterceptBeforeTx, setPhase, setRecordInterceptor, setRunning, setTopicCheckTimeout, setupMessageListener, start, stop, stop, stop
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.springframework.kafka.listener.MessageListenerContainer
destroy
-
-
-
-
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 delegateKafkaMessageListenerContainers.- 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 concurrentKafkaMessageListenerContainers 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 ofKafkaMessageListenerContainers created by this container.- Returns:
- the list of
KafkaMessageListenerContainers created by this container.
-
getAssignedPartitions
public java.util.Collection<org.apache.kafka.common.TopicPartition> getAssignedPartitions()
Description copied from interface:MessageListenerContainerReturn 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:MessageListenerContainerReturn the assigned topics/partitions for this container, by client.id.- Returns:
- the topics/partitions.
-
isContainerPaused
public boolean isContainerPaused()
Description copied from interface:MessageListenerContainerReturn 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:MessageListenerContainerIf 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:MessageListenerContainerReturn 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:
doStartin classAbstractMessageListenerContainer<K,V>
-
doStop
protected void doStop(java.lang.Runnable callback)
- Specified by:
doStopin classAbstractMessageListenerContainer<K,V>
-
pause
public void pause()
Description copied from interface:MessageListenerContainerPause this container before the next poll(). This is a thread-safe operation, the actual pause is processed by the consumer thread.- Specified by:
pausein interfaceMessageListenerContainer- Overrides:
pausein classAbstractMessageListenerContainer<K,V>- See Also:
KafkaConsumer.pause(Collection)
-
resume
public void resume()
Description copied from interface:MessageListenerContainerResume 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:
resumein interfaceMessageListenerContainer- Overrides:
resumein classAbstractMessageListenerContainer<K,V>- See Also:
KafkaConsumer.resume(Collection)
-
pausePartition
public void pausePartition(org.apache.kafka.common.TopicPartition topicPartition)
Description copied from interface:MessageListenerContainerPause this partition before the next poll(). This is a thread-safe operation, the actual pause is processed by the consumer thread.- Specified by:
pausePartitionin interfaceMessageListenerContainer- Overrides:
pausePartitionin classAbstractMessageListenerContainer<K,V>- Parameters:
topicPartition- the topicPartition to pause.
-
resumePartition
public void resumePartition(org.apache.kafka.common.TopicPartition topicPartition)
Description copied from interface:MessageListenerContainerResume 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:
resumePartitionin interfaceMessageListenerContainer- Overrides:
resumePartitionin classAbstractMessageListenerContainer<K,V>- Parameters:
topicPartition- the topicPartition to resume.
-
isPartitionPaused
public boolean isPartitionPaused(org.apache.kafka.common.TopicPartition topicPartition)
Description copied from interface:MessageListenerContainerWhether 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:
toStringin classjava.lang.Object
-
-