Class KafkaMessageListenerContainer<K,V>
- java.lang.Object
-
- org.springframework.kafka.listener.AbstractMessageListenerContainer<K,V>
-
- org.springframework.kafka.listener.KafkaMessageListenerContainer<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
,ConsumerPauseResumeEventPublisher
,GenericMessageListenerContainer<K,V>
,MessageListenerContainer
public class KafkaMessageListenerContainer<K,V> extends AbstractMessageListenerContainer<K,V> implements ConsumerPauseResumeEventPublisher
Single-threaded Message listener container using the JavaConsumer
supporting auto-partition assignment or user-configured assignment.With the latter, initial partition offsets can be provided.
-
-
Field Summary
-
Fields inherited from class org.springframework.kafka.listener.AbstractMessageListenerContainer
consumerFactory, DEFAULT_PHASE, lifecycleMonitor, logger
-
-
Constructor Summary
Constructors Constructor Description KafkaMessageListenerContainer(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 void
childStopped(MessageListenerContainer child, ConsumerStoppedEvent.Reason reason)
Notify a parent container that a child container has stopped.protected void
doStart()
protected void
doStop(java.lang.Runnable callback, boolean normal)
Stop the container normally or abnormally.java.util.Collection<org.apache.kafka.common.TopicPartition>
getAssignedPartitions()
Return theTopicPartition
s currently assigned to this container, either explicitly or by Kafka; may be null if not assigned yet.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.boolean
isContainerPaused()
Return true ifMessageListenerContainer.pause()
has been called; and all consumers in this container have actually paused.boolean
isInExpectedState()
Return true if the container is running, has never been started, or has been stopped.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
.protected AbstractMessageListenerContainer<?,?>
parentOrThis()
Return this or a parent container if this has a parent.void
pause()
Pause this container before the next poll().void
publishConsumerPausedEvent(java.util.Collection<org.apache.kafka.common.TopicPartition> partitions, java.lang.String reason)
Publish a consumer paused event.void
publishConsumerResumedEvent(java.util.Collection<org.apache.kafka.common.TopicPartition> partitions)
Publish a consumer resumed event.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
setClientIdSuffix(java.lang.String clientIdSuffix)
Set a suffix to add to theclient.id
consumer property (if the consumer factory supports it).void
setEmergencyStop(java.lang.Runnable emergencyStop)
Set aRunnable
to call whenever a fatal error occurs on the listener thread.java.lang.String
toString()
-
Methods inherited from class org.springframework.kafka.listener.AbstractMessageListenerContainer
checkGroupId, checkTopics, createSimpleLoggingConsumerRebalanceListener, doStop, getAfterRollbackProcessor, getApplicationContext, getApplicationEventPublisher, getBatchInterceptor, getBeanName, getCommonErrorHandler, getContainerProperties, getGenericErrorHandler, getGroupId, getListenerId, getListenerInfo, getPhase, getRecordInterceptor, isAutoStartup, isInterceptBeforeTx, isPartitionPauseRequested, isPaused, isPauseRequested, isRunning, isStoppedNormally, pausePartition, propertiesFromConsumerPropertyOverrides, publishContainerStoppedEvent, setAfterRollbackProcessor, setApplicationContext, setApplicationEventPublisher, setAutoStartup, setBatchErrorHandler, setBatchInterceptor, setBeanName, setCommonErrorHandler, setErrorHandler, setGenericErrorHandler, setInterceptBeforeTx, setListenerInfo, setPhase, setRecordInterceptor, setRunning, setStoppedNormally, setTopicCheckTimeout, setupMessageListener, start, stop, stop, stop, stopAbnormally
-
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, getContainerFor, isChildRunning
-
-
-
-
Constructor Detail
-
KafkaMessageListenerContainer
public KafkaMessageListenerContainer(ConsumerFactory<? super K,? super V> consumerFactory, ContainerProperties containerProperties)
Construct an instance with the supplied configuration properties.- Parameters:
consumerFactory
- the consumer factory.containerProperties
- the container properties.
-
-
Method Detail
-
setEmergencyStop
public void setEmergencyStop(java.lang.Runnable emergencyStop)
Set aRunnable
to call whenever a fatal error occurs on the listener thread.- Parameters:
emergencyStop
- the Runnable.- Since:
- 2.2.1
-
setClientIdSuffix
public void setClientIdSuffix(java.lang.String clientIdSuffix)
Set a suffix to add to theclient.id
consumer property (if the consumer factory supports it).- Parameters:
clientIdSuffix
- the suffix to add.- Since:
- 1.0.6
-
getAssignedPartitions
@Nullable public java.util.Collection<org.apache.kafka.common.TopicPartition> getAssignedPartitions()
Return theTopicPartition
s currently assigned to this container, either explicitly or by Kafka; may be null if not assigned yet.- Specified by:
getAssignedPartitions
in interfaceMessageListenerContainer
- Returns:
- the
TopicPartition
s currently assigned to this container, either explicitly or by Kafka; may be null if not assigned yet.
-
getAssignmentsByClientId
@Nullable 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.- Specified by:
getAssignmentsByClientId
in interfaceMessageListenerContainer
- 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.- Specified by:
isContainerPaused
in interfaceMessageListenerContainer
- Returns:
- true if the container is paused.
-
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.- Specified by:
isPartitionPaused
in interfaceMessageListenerContainer
- Parameters:
topicPartition
- the topic partition to check- Returns:
- true if this partition has been paused.
-
isInExpectedState
public boolean isInExpectedState()
Description copied from interface:MessageListenerContainer
Return true if the container is running, has never been started, or has been stopped.- Specified by:
isInExpectedState
in interfaceMessageListenerContainer
- Returns:
- true if the state is as expected.
- See Also:
MessageListenerContainer.stopAbnormally(Runnable)
-
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)
-
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.
-
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
.- Specified by:
metrics
in interfaceMessageListenerContainer
- 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, boolean normal)
Description copied from class:AbstractMessageListenerContainer
Stop the container normally or abnormally.- Specified by:
doStop
in classAbstractMessageListenerContainer<K,V>
- Parameters:
callback
- the callback.normal
- true for an expected stop.
-
childStopped
public void childStopped(MessageListenerContainer child, ConsumerStoppedEvent.Reason reason)
Description copied from interface:MessageListenerContainer
Notify a parent container that a child container has stopped.- Specified by:
childStopped
in interfaceMessageListenerContainer
- Parameters:
child
- the container.reason
- the reason.
-
publishConsumerPausedEvent
public void publishConsumerPausedEvent(java.util.Collection<org.apache.kafka.common.TopicPartition> partitions, java.lang.String reason)
Description copied from interface:ConsumerPauseResumeEventPublisher
Publish a consumer paused event.- Specified by:
publishConsumerPausedEvent
in interfaceConsumerPauseResumeEventPublisher
- Parameters:
partitions
- the paused partitions.reason
- the reason.
-
publishConsumerResumedEvent
public void publishConsumerResumedEvent(java.util.Collection<org.apache.kafka.common.TopicPartition> partitions)
Description copied from interface:ConsumerPauseResumeEventPublisher
Publish a consumer resumed event.- Specified by:
publishConsumerResumedEvent
in interfaceConsumerPauseResumeEventPublisher
- Parameters:
partitions
- the resumed partitions.
-
parentOrThis
protected AbstractMessageListenerContainer<?,?> parentOrThis()
Description copied from class:AbstractMessageListenerContainer
Return this or a parent container if this has a parent.- Overrides:
parentOrThis
in classAbstractMessageListenerContainer<K,V>
- Returns:
- the parent or this.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-