Class AbstractMessageListenerContainer<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
    Direct Known Subclasses:
    ConcurrentMessageListenerContainer, KafkaMessageListenerContainer

    public abstract class AbstractMessageListenerContainer<K,​V>
    extends java.lang.Object
    implements GenericMessageListenerContainer<K,​V>, org.springframework.beans.factory.BeanNameAware, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.ApplicationContextAware
    The base implementation for the MessageListenerContainer.
    Author:
    Gary Russell, Marius Bogoevici, Artem Bilan
    • Field Detail

      • DEFAULT_PHASE

        public static final int DEFAULT_PHASE
        The default SmartLifecycle phase for listener containers 2147483547.
        See Also:
        Constant Field Values
      • logger

        protected final org.springframework.core.log.LogAccessor logger
      • lifecycleMonitor

        protected final java.lang.Object lifecycleMonitor
    • Constructor Detail

      • AbstractMessageListenerContainer

        protected AbstractMessageListenerContainer​(ConsumerFactory<? super K,​? super V> consumerFactory,
                                                   ContainerProperties containerProperties)
        Construct an instance with the provided factory and properties.
        Parameters:
        consumerFactory - the factory.
        containerProperties - the properties.
    • Method Detail

      • setApplicationContext

        public void setApplicationContext​(org.springframework.context.ApplicationContext applicationContext)
                                   throws org.springframework.beans.BeansException
        Specified by:
        setApplicationContext in interface org.springframework.context.ApplicationContextAware
        Throws:
        org.springframework.beans.BeansException
      • getApplicationContext

        protected org.springframework.context.ApplicationContext getApplicationContext()
      • setBeanName

        public void setBeanName​(java.lang.String name)
        Specified by:
        setBeanName in interface org.springframework.beans.factory.BeanNameAware
      • getBeanName

        public java.lang.String getBeanName()
      • setApplicationEventPublisher

        public void setApplicationEventPublisher​(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
        Specified by:
        setApplicationEventPublisher in interface org.springframework.context.ApplicationEventPublisherAware
      • getApplicationEventPublisher

        public org.springframework.context.ApplicationEventPublisher getApplicationEventPublisher()
      • setErrorHandler

        public void setErrorHandler​(ErrorHandler errorHandler)
        Set the error handler to call when the listener throws an exception.
        Parameters:
        errorHandler - the error handler.
        Since:
        2.2
      • setGenericErrorHandler

        public void setGenericErrorHandler​(GenericErrorHandler<?> errorHandler)
        Set the error handler to call when the listener throws an exception.
        Parameters:
        errorHandler - the error handler.
        Since:
        2.2
      • setBatchErrorHandler

        public void setBatchErrorHandler​(BatchErrorHandler errorHandler)
        Set the batch error handler to call when the listener throws an exception.
        Parameters:
        errorHandler - the error handler.
        Since:
        2.2
      • getGenericErrorHandler

        public GenericErrorHandler<?> getGenericErrorHandler()
        Get the configured error handler.
        Returns:
        the error handler.
        Since:
        2.2
      • isAutoStartup

        public boolean isAutoStartup()
        Specified by:
        isAutoStartup in interface org.springframework.context.SmartLifecycle
      • setRunning

        protected void setRunning​(boolean running)
      • isRunning

        public boolean isRunning()
        Specified by:
        isRunning in interface org.springframework.context.Lifecycle
      • isPaused

        protected boolean isPaused()
      • setPhase

        public void setPhase​(int phase)
      • getPhase

        public int getPhase()
        Specified by:
        getPhase in interface org.springframework.context.Phased
        Specified by:
        getPhase in interface org.springframework.context.SmartLifecycle
      • setAfterRollbackProcessor

        public void setAfterRollbackProcessor​(AfterRollbackProcessor<? super K,​? super V> afterRollbackProcessor)
        Set a processor to perform seeks on unprocessed records after a rollback. Default will seek to current position all topics/partitions, including the failed record.
        Parameters:
        afterRollbackProcessor - the processor.
        Since:
        1.3.5
      • getGroupId

        public java.lang.String getGroupId()
        Description copied from interface: MessageListenerContainer
        Return the group.id property for this container whether specifically set on the container or via a consumer property on the consumer factory.
        Specified by:
        getGroupId in interface MessageListenerContainer
        Returns:
        the group id.
      • getListenerId

        @Nullable
        public java.lang.String getListenerId()
        Description copied from interface: MessageListenerContainer
        The 'id' attribute of a @KafkaListener or the bean name for spring-managed containers.
        Specified by:
        getListenerId in interface MessageListenerContainer
        Returns:
        the id or bean name.
      • setTopicCheckTimeout

        public void setTopicCheckTimeout​(int topicCheckTimeout)
        How long to wait for Admin.describeTopics(Collection) result futures to complete.
        Parameters:
        topicCheckTimeout - the timeout in seconds; default 30.
        Since:
        2.3
      • setRecordInterceptor

        public void setRecordInterceptor​(RecordInterceptor<K,​V> recordInterceptor)
        Set an interceptor to be called before calling the record listener. Does not apply to batch listeners.
        Parameters:
        recordInterceptor - the interceptor.
        Since:
        2.2.7
        See Also:
        setInterceptBeforeTx(boolean)
      • setBatchInterceptor

        public void setBatchInterceptor​(BatchInterceptor<K,​V> batchInterceptor)
        Set an interceptor to be called before calling the record listener. Does not apply to batch listeners.
        Parameters:
        batchInterceptor - the interceptor.
        Since:
        2.6.8
        See Also:
        setInterceptBeforeTx(boolean)
      • isInterceptBeforeTx

        protected boolean isInterceptBeforeTx()
      • setupMessageListener

        public void setupMessageListener​(java.lang.Object messageListener)
        Description copied from interface: MessageListenerContainer
        Setup the message listener to use. Throws an IllegalArgumentException if that message listener type is not supported.
        Specified by:
        setupMessageListener in interface MessageListenerContainer
        Parameters:
        messageListener - the object to wrapped to the MessageListener.
      • start

        public final void start()
        Specified by:
        start in interface org.springframework.context.Lifecycle
      • checkTopics

        protected void checkTopics()
      • checkGroupId

        public void checkGroupId()
      • doStart

        protected abstract void doStart()
      • stop

        public final void stop()
        Specified by:
        stop in interface org.springframework.context.Lifecycle
      • stop

        public final void stop​(boolean wait)
        Stop the container.
        Parameters:
        wait - wait for the listener to terminate.
        Since:
        2.3.8
      • 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
        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
        See Also:
        KafkaConsumer.resume(Collection)
      • stop

        public void stop​(java.lang.Runnable callback)
        Specified by:
        stop in interface org.springframework.context.SmartLifecycle
      • doStop

        protected abstract void doStop​(java.lang.Runnable callback)
      • createSimpleLoggingConsumerRebalanceListener

        protected final org.apache.kafka.clients.consumer.ConsumerRebalanceListener createSimpleLoggingConsumerRebalanceListener()
        Return default implementation of ConsumerRebalanceListener instance.
        Returns:
        the ConsumerRebalanceListener currently assigned to this container.
      • publishContainerStoppedEvent

        protected void publishContainerStoppedEvent()
      • parentOrThis

        protected AbstractMessageListenerContainer<?,​?> parentOrThis()
        Return this or a parent container if this has a parent.
        Returns:
        the parent or this.
        Since:
        2.2.1