Skip to content

Instantly share code, notes, and snippets.

@recursivecodes
Created October 9, 2019 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recursivecodes/ea65c4810d3e26626e752ab5fabd2f75 to your computer and use it in GitHub Desktop.
Save recursivecodes/ea65c4810d3e26626e752ab5fabd2f75 to your computer and use it in GitHub Desktop.
CompatibleConsumer.java
try {
    consumer.subscribe(Collections.singletonList( topicName ) );
    while(true) {
        Duration duration = Duration.ofMillis(1000);
        ConsumerRecords<Long, String> consumerRecords = consumer.poll(duration);
        consumerRecords.forEach(record -> {
            System.out.println("Record Key " + record.key());
            System.out.println("Record value " + record.value());
            System.out.println("Record partition " + record.partition());
            System.out.println("Record offset " + record.offset());
        });
        // commits the offset of record to broker.
        consumer.commitAsync();
    }
}
catch(WakeupException e) {
    // do nothing, shutting down...
}
finally {
    System.out.println("closing consumer");
    consumer.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment