Skip to content

Instantly share code, notes, and snippets.

@recursivecodes
Created October 9, 2019 13:45
Show Gist options
  • Save recursivecodes/fe0ed9c857a9f96ba266fa3821edc906 to your computer and use it in GitHub Desktop.
Save recursivecodes/fe0ed9c857a9f96ba266fa3821edc906 to your computer and use it in GitHub Desktop.
CompatibleProducer.java
KafkaProducer producer = new KafkaProducer<>(properties);
for (int i = 0; i < 5; i++) {
    ProducerRecord<String, String> record = new ProducerRecord<>(topicName, UUID.randomUUID().toString(), "Test record #" + i);
    producer.send(record, (md, ex) -> {
        if( ex != null ) {
            ex.printStackTrace();
        }
        else {
            System.out.println(
                    "Sent msg to "
                            + md.partition()
                            + " with offset "
                            + md.offset()
                            + " at "
                            + md.timestamp()
            );
        }
    });
}
producer.flush();
producer.close();
System.out.println("produced 5 messages");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment