Class KafkaSender<K extends String,​V extends Serializable>

  • Type Parameters:
    K - type of key
    V - type of value

    @Component
    @ConditionalOnClass(org.springframework.kafka.core.KafkaTemplate.class)
    @EnableConfigurationProperties(org.birchframework.configuration.BirchProperties.class)
    public class KafkaSender<K extends String,​V extends Serializable>
    extends Object
    Customized Kafka producer. Supports:
    • Synchronous
    • Synchronous transactional
    • Asynchronous
    • Asynchronous transactional

    Available configurations are:

     birch:
       kafka:
         sender:
           wait-time: 2s                    # duration of wait time to wait for response when sending synchronously; default is 2 seconds
           allow-non-transactional: true    # whether to configure the Kafka template to allows sending outside of a transaction context; default is true
     
    Author:
    Keivan Khalichi
    • Constructor Summary

      Constructors 
      Constructor Description
      KafkaSender​(org.springframework.kafka.core.KafkaTemplate<K,​V> theTemplate, BirchProperties theProperties)
      Construct this sender using the provided KafkaTemplate and BirchProperties.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Optional<KafkaSendResult<K,​V>> send​(String topic, Integer partition, K key, V data)
      Send message, synchronously.
      Optional<KafkaSendResult<K,​V>> send​(String topic, K key, V data)
      Send message, synchronously.
      Optional<KafkaSendResult<K,​V>> send​(String topic, V data)
      Send message, synchronously.
      void sendAsync​(String topic, Integer partition, K key, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)
      Sends message to topic asynchronously, not transactional, and with provided callbacks.
      void sendAsync​(String topic, K key, V data)
      Method to send message, asynchronously.
      void sendAsync​(String topic, K key, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)
      Sends message to topic asynchronously with provided callbacks.
      void sendAsync​(String topic, V data)
      Sends message to topic asynchronously.
      void sendAsync​(String topic, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)
      Sends message to topic asynchronously with provided callbacks.
      void sendAsyncTransactional​(String topic, Integer partition, K key, V data, org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback, org.springframework.util.concurrent.FailureCallback failureCallback)
      Sends message to topic asynchronously and within a Kafka transaction, with provided callbacks.
      Optional<KafkaSendResult<K,​V>> sendTransactional​(String topic, Integer partition, K key, V data)
      Send message synchronously and within a transaction.
    • Constructor Detail

      • KafkaSender

        public KafkaSender​(org.springframework.kafka.core.KafkaTemplate<K,​V> theTemplate,
                           BirchProperties theProperties)
        Construct this sender using the provided KafkaTemplate and BirchProperties. By default, the instance is not transactional.
    • Method Detail

      • sendTransactional

        public Optional<KafkaSendResult<K,​V>> sendTransactional​(@Nonnull
                                                                      String topic,
                                                                      @Nullable
                                                                      Integer partition,
                                                                      @Nullable
                                                                      K key,
                                                                      @Nonnull
                                                                      V data)
        Send message synchronously and within a transaction.
        Parameters:
        topic - topic name
        key - message key
        data - a message payload implementing Serializable
        Returns:
        result, if there are no exceptions
        Throws:
        InterruptedException - rethrow when waiting to get results throws the exception
      • sendAsync

        public void sendAsync​(String topic,
                              V data)
        Sends message to topic asynchronously.
        Parameters:
        topic - the topic
        data - a message payload implementing Serializable
      • sendAsync

        public void sendAsync​(String topic,
                              V data,
                              org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback,
                              org.springframework.util.concurrent.FailureCallback failureCallback)
        Sends message to topic asynchronously with provided callbacks.
        Parameters:
        topic - the topic
        data - a message payload implementing Serializable
        successCallback - the callback to call upon success
      • sendAsync

        public void sendAsync​(String topic,
                              K key,
                              V data)
        Method to send message, asynchronously.
        Parameters:
        topic - topic name
        key - message key
        data - a message payload implementing Serializable
      • sendAsync

        public void sendAsync​(String topic,
                              K key,
                              V data,
                              org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback,
                              org.springframework.util.concurrent.FailureCallback failureCallback)
        Sends message to topic asynchronously with provided callbacks.
        Parameters:
        topic - the topic
        key - the message key
        data - a message payload implementing Serializable
        successCallback - the callback to call upon success
      • sendAsync

        public void sendAsync​(@Nonnull
                              String topic,
                              @Nullable
                              Integer partition,
                              @Nullable
                              K key,
                              @Nonnull
                              V data,
                              @Nonnull
                              org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback,
                              org.springframework.util.concurrent.FailureCallback failureCallback)
        Sends message to topic asynchronously, not transactional, and with provided callbacks.
        Parameters:
        topic - the topic
        partition - the topic partition
        key - the key
        data - a message payload implementing Serializable
        successCallback - the callback to call upon success
      • sendAsyncTransactional

        public void sendAsyncTransactional​(@Nonnull
                                           String topic,
                                           @Nullable
                                           Integer partition,
                                           @Nullable
                                           K key,
                                           @Nonnull
                                           V data,
                                           @Nonnull
                                           org.springframework.util.concurrent.SuccessCallback<org.springframework.kafka.support.SendResult<K,​V>> successCallback,
                                           org.springframework.util.concurrent.FailureCallback failureCallback)
        Sends message to topic asynchronously and within a Kafka transaction, with provided callbacks.
        Parameters:
        topic - the topic
        partition - the topic partition
        key - the key
        data - a message payload implementing Serializable
        successCallback - the callback to call upon success