Class KafkaUtils


  • public final class KafkaUtils
    extends java.lang.Object
    Utility methods.
    Since:
    2.2
    Author:
    Gary Russell
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean MICROMETER_PRESENT
      True if micrometer is on the class path.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void clearConsumerGroupId()
      Clear the group id for the consumer bound to this thread.
      static java.time.Duration determineSendTimeout​(java.util.Map<java.lang.String,​java.lang.Object> producerProps, long buffer, long min)
      Return the timeout to use when sending records.
      static java.lang.String format​(org.apache.kafka.clients.consumer.ConsumerRecord<?,​?> record)
      Format the ConsumerRecord for logging; default [email protected].
      static java.lang.String format​(org.apache.kafka.clients.consumer.ConsumerRecord<?,​?> record, boolean full)
      Deprecated.
      static java.lang.String format​(org.apache.kafka.clients.producer.ProducerRecord<?,​?> record)
      Format the ProducerRecord for logging; default ProducerRecordObject.toString().
      static java.lang.String getConsumerGroupId()
      Get the group id for the consumer bound to this thread.
      static boolean returnTypeMessageOrCollectionOf​(java.lang.reflect.Method method)
      Return true if the method return type is Message or Collection<Message<?>>.
      static void setConsumerGroupId​(java.lang.String groupId)
      Set the group id for the consumer bound to this thread.
      static void setConsumerRecordFormatter​(java.util.function.Function<org.apache.kafka.clients.consumer.ConsumerRecord<?,​?>,​java.lang.String> formatter)
      Set a formatter for logging ConsumerRecords; default is [email protected].
      static void setProducerRecordFormatter​(java.util.function.Function<org.apache.kafka.clients.producer.ProducerRecord<?,​?>,​java.lang.String> formatter)
      Set a formatter for logging ProducerRecords; default is ProducerRecord.toString().
      • Methods inherited from class java.lang.Object

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

      • MICROMETER_PRESENT

        public static final boolean MICROMETER_PRESENT
        True if micrometer is on the class path.
    • Method Detail

      • returnTypeMessageOrCollectionOf

        public static boolean returnTypeMessageOrCollectionOf​(java.lang.reflect.Method method)
        Return true if the method return type is Message or Collection<Message<?>>.
        Parameters:
        method - the method.
        Returns:
        true if it returns message(s).
      • setConsumerGroupId

        public static void setConsumerGroupId​(java.lang.String groupId)
        Set the group id for the consumer bound to this thread.
        Parameters:
        groupId - the group id.
        Since:
        2.3
      • getConsumerGroupId

        public static java.lang.String getConsumerGroupId()
        Get the group id for the consumer bound to this thread.
        Returns:
        the group id.
        Since:
        2.3
      • clearConsumerGroupId

        public static void clearConsumerGroupId()
        Clear the group id for the consumer bound to this thread.
        Since:
        2.3
      • determineSendTimeout

        public static java.time.Duration determineSendTimeout​(java.util.Map<java.lang.String,​java.lang.Object> producerProps,
                                                              long buffer,
                                                              long min)
        Return the timeout to use when sending records. If the ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG is not configured, or is not a number or a String that can be parsed as a long, the ProducerConfig default value (plus the buffer) is used.
        Parameters:
        producerProps - the producer properties.
        buffer - a buffer to add to the configured ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG to prevent timing out before the Kafka producer.
        min - a minimum value to apply after adding the buffer to the configured timeout.
        Returns:
        the timeout to use.
        Since:
        2.7
      • setConsumerRecordFormatter

        public static void setConsumerRecordFormatter​(java.util.function.Function<org.apache.kafka.clients.consumer.ConsumerRecord<?,​?>,​java.lang.String> formatter)
        Set a formatter for logging ConsumerRecords; default is [email protected].
        Parameters:
        formatter - a function to format the record as a String
        Since:
        2.7.12
      • setProducerRecordFormatter

        public static void setProducerRecordFormatter​(java.util.function.Function<org.apache.kafka.clients.producer.ProducerRecord<?,​?>,​java.lang.String> formatter)
        Set a formatter for logging ProducerRecords; default is ProducerRecord.toString().
        Parameters:
        formatter - a function to format the record as a String
        Since:
        2.7.12
      • format

        public static java.lang.String format​(org.apache.kafka.clients.consumer.ConsumerRecord<?,​?> record)
        Format the ConsumerRecord for logging; default [email protected].
        Parameters:
        record - the record to format.
        Returns:
        the formatted String.
        Since:
        2.7.12
      • format

        @Deprecated
        public static java.lang.String format​(org.apache.kafka.clients.consumer.ConsumerRecord<?,​?> record,
                                              boolean full)
        Deprecated.
        Format the ConsumerRecord for logging; default [email protected]. Provided for backwards compatibility only.
        Parameters:
        record - the record to format.
        full - use ConsumerRecord.toString().
        Returns:
        the formatted String.
        Since:
        2.7.12
      • format

        public static java.lang.String format​(org.apache.kafka.clients.producer.ProducerRecord<?,​?> record)
        Format the ProducerRecord for logging; default ProducerRecordObject.toString().
        Parameters:
        record - the record to format.
        Returns:
        the formatted String.
        Since:
        2.7.12