Class KafkaAdmin

  • All Implemented Interfaces:
    org.springframework.beans.factory.Aware, org.springframework.beans.factory.SmartInitializingSingleton, org.springframework.context.ApplicationContextAware, KafkaAdminOperations

    public class KafkaAdmin
    extends KafkaResourceFactory
    implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.SmartInitializingSingleton, KafkaAdminOperations
    An admin that delegates to an AdminClient to create topics defined in the application context.
    Since:
    1.3
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  KafkaAdmin.NewTopics
      Wrapper for a collection of NewTopic to facilitate declaring multiple topics as a single bean.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.time.Duration DEFAULT_CLOSE_TIMEOUT
      The default close timeout duration as 10 seconds.
    • Constructor Summary

      Constructors 
      Constructor Description
      KafkaAdmin​(java.util.Map<java.lang.String,​java.lang.Object> config)
      Create an instance with an AdminClient based on the supplied configuration.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterSingletonsInstantiated()  
      void createOrModifyTopics​(org.apache.kafka.clients.admin.NewTopic... topics)
      Create topics if they don't exist or increase the number of partitions if needed.
      java.util.Map<java.lang.String,​org.apache.kafka.clients.admin.TopicDescription> describeTopics​(java.lang.String... topicNames)
      Obtain TopicDescriptions for these topics.
      java.util.Map<java.lang.String,​java.lang.Object> getConfigurationProperties()
      Get an unmodifiable copy of this admin's configuration.
      protected java.util.function.Predicate<org.apache.kafka.clients.admin.NewTopic> getCreateOrModifyTopic()
      Return the predicate used to determine whether a NewTopic should be considered for creation or modification.
      boolean initialize()
      Call this method to check/add topics; this might be needed if the broker was not available when the application context was initialized, and fatalIfBrokerNotAvailable is false, or autoCreate was set to false.
      protected java.util.Collection<org.apache.kafka.clients.admin.NewTopic> newTopics()
      Return a collection of NewTopics to create or modify.
      void setApplicationContext​(org.springframework.context.ApplicationContext applicationContext)  
      void setAutoCreate​(boolean autoCreate)
      Set to false to suppress auto creation of topics during context initialization.
      void setCloseTimeout​(int closeTimeout)
      Set the close timeout in seconds.
      void setCreateOrModifyTopic​(java.util.function.Predicate<org.apache.kafka.clients.admin.NewTopic> createOrModifyTopic)
      Set a predicate that returns true if a discovered NewTopic bean should be considered for creation or modification by this admin instance.
      void setFatalIfBrokerNotAvailable​(boolean fatalIfBrokerNotAvailable)
      Set to true if you want the application context to fail to load if we are unable to connect to the broker during initialization, to check/add topics.
      void setModifyTopicConfigs​(boolean modifyTopicConfigs)
      Set to true to compare the current topic configuration properties with those in the NewTopic bean, and update if different.
      void setOperationTimeout​(int operationTimeout)
      Set the operation timeout in seconds.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_CLOSE_TIMEOUT

        public static final java.time.Duration DEFAULT_CLOSE_TIMEOUT
        The default close timeout duration as 10 seconds.
    • Constructor Detail

      • KafkaAdmin

        public KafkaAdmin​(java.util.Map<java.lang.String,​java.lang.Object> config)
        Create an instance with an AdminClient based on the supplied configuration.
        Parameters:
        config - the configuration for the AdminClient.
    • 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
      • setCloseTimeout

        public void setCloseTimeout​(int closeTimeout)
        Set the close timeout in seconds. Defaults to DEFAULT_CLOSE_TIMEOUT seconds.
        Parameters:
        closeTimeout - the timeout.
      • setOperationTimeout

        public void setOperationTimeout​(int operationTimeout)
        Set the operation timeout in seconds. Defaults to 30 seconds.
        Parameters:
        operationTimeout - the timeout.
      • setFatalIfBrokerNotAvailable

        public void setFatalIfBrokerNotAvailable​(boolean fatalIfBrokerNotAvailable)
        Set to true if you want the application context to fail to load if we are unable to connect to the broker during initialization, to check/add topics.
        Parameters:
        fatalIfBrokerNotAvailable - true to fail.
      • setAutoCreate

        public void setAutoCreate​(boolean autoCreate)
        Set to false to suppress auto creation of topics during context initialization.
        Parameters:
        autoCreate - boolean flag to indicate creating topics or not during context initialization
        See Also:
        initialize()
      • setModifyTopicConfigs

        public void setModifyTopicConfigs​(boolean modifyTopicConfigs)
        Set to true to compare the current topic configuration properties with those in the NewTopic bean, and update if different.
        Parameters:
        modifyTopicConfigs - true to check and update configs if necessary.
        Since:
        2.8.7
      • setCreateOrModifyTopic

        public void setCreateOrModifyTopic​(java.util.function.Predicate<org.apache.kafka.clients.admin.NewTopic> createOrModifyTopic)
        Set a predicate that returns true if a discovered NewTopic bean should be considered for creation or modification by this admin instance. The default predicate returns true for all NewTopics. Used by the default implementation of newTopics().
        Parameters:
        createOrModifyTopic - the predicate.
        Since:
        2.9.10
        See Also:
        newTopics()
      • getCreateOrModifyTopic

        protected java.util.function.Predicate<org.apache.kafka.clients.admin.NewTopic> getCreateOrModifyTopic()
        Return the predicate used to determine whether a NewTopic should be considered for creation or modification.
        Returns:
        the predicate.
        Since:
        2.9.10
        See Also:
        newTopics()
      • getConfigurationProperties

        public java.util.Map<java.lang.String,​java.lang.Object> getConfigurationProperties()
        Description copied from interface: KafkaAdminOperations
        Get an unmodifiable copy of this admin's configuration.
        Specified by:
        getConfigurationProperties in interface KafkaAdminOperations
        Returns:
        the configuration map.
      • afterSingletonsInstantiated

        public void afterSingletonsInstantiated()
        Specified by:
        afterSingletonsInstantiated in interface org.springframework.beans.factory.SmartInitializingSingleton
      • newTopics

        protected java.util.Collection<org.apache.kafka.clients.admin.NewTopic> newTopics()
        Return a collection of NewTopics to create or modify. The default implementation retrieves all NewTopic beans in the application context and applies the setCreateOrModifyTopic(Predicate) predicate to each one. It also removes any TopicForRetryable bean if there is also a NewTopic with the same topic name. This is performed before calling the predicate.
        Returns:
        the collection of NewTopics.
        Since:
        2.9.10
        See Also:
        setCreateOrModifyTopic(Predicate)
      • createOrModifyTopics

        public void createOrModifyTopics​(org.apache.kafka.clients.admin.NewTopic... topics)
        Description copied from interface: KafkaAdminOperations
        Create topics if they don't exist or increase the number of partitions if needed.
        Specified by:
        createOrModifyTopics in interface KafkaAdminOperations
        Parameters:
        topics - the topics.
      • describeTopics

        public java.util.Map<java.lang.String,​org.apache.kafka.clients.admin.TopicDescription> describeTopics​(java.lang.String... topicNames)
        Description copied from interface: KafkaAdminOperations
        Obtain TopicDescriptions for these topics.
        Specified by:
        describeTopics in interface KafkaAdminOperations
        Parameters:
        topicNames - the topic names.
        Returns:
        a map of name:topicDescription.