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:
Aware, BeanNameAware, DisposableBean, ApplicationContextAware, ApplicationEventPublisherAware, Lifecycle, Phased, SmartLifecycle, GenericMessageListenerContainer<K,V>, MessageListenerContainer

public class KafkaMessageListenerContainer<K,V> extends AbstractMessageListenerContainer<K,V>
Single-threaded Message listener container using the Java Consumer supporting auto-partition assignment or user-configured assignment.

With the latter, initial partition offsets can be provided.

Author:
Gary Russell, Murali Reddy, Marius Bogoevici, Martin Dam, Artem Bilan, Loic Talhouarne, Vladimir Tsanev, Chen Binbin, Yang Qiju, Tom van den Berge, Lukasz Kaminski, Tomaz Fernandes
  • Constructor Details

    • 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 Details

    • setEmergencyStop

      public void setEmergencyStop(Runnable emergencyStop)
      Set a Runnable to call whenever an Error occurs on a listener thread.
      Parameters:
      emergencyStop - the Runnable.
      Since:
      2.2.1
    • setClientIdSuffix

      public void setClientIdSuffix(String clientIdSuffix)
      Set a suffix to add to the client.id consumer property (if the consumer factory supports it).
      Parameters:
      clientIdSuffix - the suffix to add.
      Since:
      1.0.6
    • getAssignedPartitions

      @Nullable public Collection<org.apache.kafka.common.TopicPartition> getAssignedPartitions()
      Return the TopicPartitions currently assigned to this container, either explicitly or by Kafka; may be null if not assigned yet.
      Returns:
      the TopicPartitions currently assigned to this container, either explicitly or by Kafka; may be null if not assigned yet.
    • getAssignmentsByClientId

      @Nullable public Map<String,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 if MessageListenerContainer.pause() has been called; and all consumers in this container have actually paused.
      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.
      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.
      Returns:
      true if the state is as expected.
      See Also:
    • 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 interface MessageListenerContainer
      Overrides:
      pause in class AbstractMessageListenerContainer<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 interface MessageListenerContainer
      Overrides:
      resume in class AbstractMessageListenerContainer<K,V>
      See Also:
      • KafkaConsumer.resume(Collection)
    • metrics

      public Map<String,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 by client-id.
      Returns:
      the consumer(s) metrics grouped by client-id
      See Also:
      • Consumer.metrics()
    • doStart

      protected void doStart()
      Specified by:
      doStart in class AbstractMessageListenerContainer<K,V>
    • doStop

      protected void doStop(Runnable callback, boolean normal)
      Description copied from class: AbstractMessageListenerContainer
      Stop the container normally or abnormally.
      Specified by:
      doStop in class AbstractMessageListenerContainer<K,V>
      Parameters:
      callback - the callback.
      normal - true for an expected stop.
    • parentOrThis

      protected AbstractMessageListenerContainer<?,?> parentOrThis()
      Description copied from class: AbstractMessageListenerContainer
      Return this or a parent container if this has a parent.
      Overrides:
      parentOrThis in class AbstractMessageListenerContainer<K,V>
      Returns:
      the parent or this.
    • toString

      public String toString()
      Overrides:
      toString in class Object