Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Kafka-Difference between configuring KafkaTemplate with Producer Listener and registering a callback with Listenable Future

So I was going through the Spring kafka documentation and came across Producer Listener. This is what the Spring Kafka Documentation says-

"Optionally, you can configure the KafkaTemplate with a ProducerListener to get an async callback with the results of the send (success or failure) instead of waiting for the Future to complete."

They have also specified the interface-

     public interface ProducerListener<K, V> {

      void onSuccess(String topic, Integer partition, K key, V value, 
      RecordMetadata recordMetadata);

      void onError(String topic, Integer partition, K key, V value, 
      Exception exception);

      boolean isInterestedInSuccess();

  }

So my understanding is, if you want to do something on success and on failure of a message, implement the interface ProducerListener and register that with the KafkaTemplate. It is asynchronous, so you wont have to wait for the future to complete, to know the outcome of your send operation.

About 3 sentences below this, its mentioned that you can also add a callback with the ListenableFuture returned by send methods of KakfaTemplate. This is also asynchronous.

 future.addCallback(new ListenableFutureCallback<SendResult<Integer, 
   String>>() {

 @Override
 public void onSuccess(SendResult<Integer, String> result) {
    ...
  }

  @Override
  public void onFailure(Throwable ex) {
    ...
  }

  });

So i wanted to know what is the exact difference between the two, because they both are asynchronous. Is the difference between what data is received in onSuccess and onFailure/onError methods. Or was the capability of adding a ProducerListener to a KafkaTemplate developed before the provision of adding a callback to a ListenableFuture (because one could not know the outcome of a asynchronous computation without blocking it- get() method in a future) or vice-versa. Hence, just to ensure backward compatibility, both were allowed to stay on. Is there any performance advantage of using one approach over other.

Thank you in advance.

like image 472
Indraneel Bende Avatar asked Jan 21 '18 05:01

Indraneel Bende


People also ask

What is the difference between Kafka listener and consumer?

They are not interchangeable in Java code. KafkaConsumer is a class from the Kafka client library which provides the API for applications to receive messages. KafkaListener is an annotation applied to a method so Spring Kafka will invoke it to process a message.

What is KafkaTemplate used for?

Spring for Apache Kafka's KafkaTemplate is a thin wrapper around a Kafka producer that plays nicely with other Spring features, like dependency injection and automatic configuration. It provides a number of convenience methods for producing to Kafka topics.

Is KafkaTemplate thread safe?

Yes, KafkaTemplate is designed to be thread-safe. If you look at its source code, you see the following member variable declarations: protected final Log logger = LogFactory.


1 Answers

Your guess is "almost" correct - the ProducerListener concept pre-dated the KafkaTemplate (in an earlier spring-integration-kafka project that pre-dated spring-kafka - that project is now based on spring-kafka).

However, it still has a place, if you are only interested passively - e.g. the LoggingProducerListener.

Adding a callback to the ListenableFuture is more applicable if you need to know the result in the context of the calling code....

  • perform a template operation
  • add a callback with a latch that is counted down (and maybe logs)
  • do some more work
  • wait for the latch to count down

Of course, you can get the same effect more simply with...

  • perform a template operation
  • do some more work
  • get the future result

...but there may be cases where the first approach is preferred, such as doing multiple operations and waiting for them all to complete.

There is no real performance difference.

like image 157
Gary Russell Avatar answered Oct 13 '22 21:10

Gary Russell