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

Indraneel Bende :

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.

Gary Russell :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=437859&siteId=1