Guest User

Untitled

a guest
Nov 21st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. package test;
  2.  
  3. import org.apache.kafka.clients.producer.KafkaProducer;
  4. import org.apache.kafka.clients.producer.ProducerConfig;
  5. import org.apache.kafka.clients.producer.ProducerRecord;
  6.  
  7. import java.util.Properties;
  8.  
  9.  
  10. class kafkatest {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. Properties props = new Properties();
  15. props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
  16. props.put(ProducerConfig.CLIENT_ID_CONFIG, "hello-world-producer");
  17. props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
  18. props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "test");
  19. props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
  20. props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
  21.  
  22. KafkaProducer producer = new KafkaProducer(props);
  23.  
  24. producer.initTransactions();
  25. producer.beginTransaction();
  26.  
  27. producer.send(new ProducerRecord<>("topic", "hello", "world"));
  28. producer.flush();
  29.  
  30. producer.abortTransaction();
  31.  
  32. producer.close();
  33. }
  34. }
  35.  
  36. --- ~ » kafka-console-consumer --bootstrap-server localhost:9092
  37. --topic topic
  38. --from-beginning
  39. --consumer-property isolation.level=read_committed
  40.  
  41. world
Add Comment
Please, Sign In to add comment