mirror of https://github.com/apache/kafka.git
KAFKA-2478: Fix manual committing example in javadoc
Committing before inserting all records into the database might lead to some records being lost. I've changed the example to commit only after all records returned by `poll` are inserted into the database. Author: Dmitry Stratiychuk <dstratiychuk@yammer-inc.com> Reviewers: Jason Gustafson, Guozhang Wang Closes #210 from shtratos/KAFKA-2478
This commit is contained in:
parent
5ae97196ae
commit
82c2191490
|
@ -145,7 +145,7 @@ import java.util.regex.Pattern;
|
|||
* props.put("session.timeout.ms", "30000");
|
||||
* props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
|
||||
* props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
|
||||
* KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
|
||||
* KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
|
||||
* consumer.subscribe(Arrays.asList("foo", "bar"));
|
||||
* while (true) {
|
||||
* ConsumerRecords<String, String> records = consumer.poll(100);
|
||||
|
@ -200,19 +200,19 @@ import java.util.regex.Pattern;
|
|||
* props.put("session.timeout.ms", "30000");
|
||||
* props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
|
||||
* props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
|
||||
* KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
|
||||
* KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
|
||||
* consumer.subscribe(Arrays.asList("foo", "bar"));
|
||||
* int commitInterval = 200;
|
||||
* List<ConsumerRecord<String, String>> buffer = new ArrayList<ConsumerRecord<String, String>>();
|
||||
* final int minBatchSize = 200;
|
||||
* List<ConsumerRecord<String, String>> buffer = new ArrayList<>();
|
||||
* while (true) {
|
||||
* ConsumerRecords<String, String> records = consumer.poll(100);
|
||||
* for (ConsumerRecord<String, String> record : records) {
|
||||
* buffer.add(record);
|
||||
* if (buffer.size() >= commitInterval) {
|
||||
* insertIntoDb(buffer);
|
||||
* consumer.commitSync();
|
||||
* buffer.clear();
|
||||
* }
|
||||
* }
|
||||
* if (buffer.size() >= minBatchSize) {
|
||||
* insertIntoDb(buffer);
|
||||
* consumer.commitSync();
|
||||
* buffer.clear();
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
|
|
Loading…
Reference in New Issue