Advertisement
Guest User

MyOwnErrorCallback

a guest
May 7th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package test;
  2.  
  3. /**
  4.  * Created by rahuljai on 5/7/15.
  5.  */
  6. import org.apache.kafka.clients.producer.Callback;
  7. import org.apache.kafka.clients.producer.RecordMetadata;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10.  
  11. public class MyOwnErrorCallback implements Callback {
  12.     private String topic;
  13.     private byte[] key;
  14.     private byte[] value;
  15.     private boolean logAsString;
  16.  
  17.     public MyOwnErrorCallback(String topic, byte[] key, byte[] value, boolean logAsString) {
  18.         this.topic = topic;
  19.         this.key = key;
  20.         this.value = value;
  21.         this.logAsString = logAsString;
  22.     }
  23.  
  24.     public void onCompletion(RecordMetadata metadata, Exception e) {
  25.         if(e != null) {
  26.             String keyString = this.key == null?"null":(this.logAsString?new String(this.key):this.key.length + " bytes");
  27.             String valueString = this.value == null?"null":(this.logAsString?new String(this.value):this.value.length + " bytes");
  28.  
  29.  
  30.             String msg = String.format("Error when sending message to topic %s with key: %s, value: %s with error: %s", new Object[]{this.topic, keyString, valueString, e.getMessage()});
  31.  
  32.             System.out.println(msg);
  33.         }
  34.  
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement