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 moreKafkaMessageListenerContainers based onconcurrency. If theContainerPropertiesis configured withTopicPartitions, theTopicPartitions are distributed evenly across the instances.
-
-
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.booleanisContainerPaused()Return true ifMessageListenerContainer.pause()has been called; and all consumers in this container have actually 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().voidresume()Resume this container, 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, getBeanName, getContainerProperties, getGenericErrorHandler, getGroupId, getListenerId, getPhase, getRecordInterceptor, isAutoStartup, isInterceptBeforeTx, isPaused, isPauseRequested, isRunning, parentOrThis, publishContainerStoppedEvent, setAfterRollbackProcessor, setApplicationContext, setApplicationEventPublisher, setAutoStartup, setBatchErrorHandler, setBeanName, 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 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.
-
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)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-