showuon commented on code in PR #15561:
URL: https://github.com/apache/kafka/pull/15561#discussion_r1533535316
##########
examples/src/main/java/kafka/examples/ExactlyOnceMessageProcessor.java:
##########
@@ -215,6 +238,39 @@ private long getRemainingRecords(KafkaConsumer<Integer,
String> consumer) {
}).sum();
}
+ /**
+ * When we get a generic {@code KafkaException} while processing records,
we retry up to {@code MAX_RETRIES} times.
+ * If we exceed this threshold, we log and error and move on to the next
batch of records.
+ * In a real world application you may want to to send these records to a
DLQ for further processing.
+ *
+ * @param retries Current number of retries
+ * @param consumer Consumer instance
+ * @return Updated number of retries
+ */
+ private int maybeRetry(int retries, KafkaConsumer<Integer, String>
consumer) {
+ if (retries < MAX_RETRIES) {
+ // retry: reset fetch offset
+ // the consumer fetch position needs to be restored to the
committed offset before the transaction started
+ Map<TopicPartition, OffsetAndMetadata> committed =
consumer.committed(consumer.assignment());
+ consumer.assignment().forEach(tp -> {
+ OffsetAndMetadata offsetAndMetadata = committed.get(tp);
+ if (offsetAndMetadata != null) {
+ consumer.seek(tp, offsetAndMetadata.offset());
+ } else {
+ consumer.seekToBeginning(Collections.singleton(tp));
+ }
+ });
+ retries++;
+ } else if (retries >= MAX_RETRIES) {
Review Comment:
OK, so we can just do `else` directly, not `else if` here, right?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]