Source:
private void controlLoop() { try { while (running) { boolean updatedAssignment = false; if (topics.size() == 0) { commitPendingAndWaitForTopics(); } if (topicsChanged) { commitPendingOffsetsForRemovedTopics(); kafkaConsumer.subscribe(topics, new RebalanceListener()); updatedAssignment = true; } ConsumerRecords<?, ?> records = flowControlledPoll(); if (updatedAssignment) { updateAssignment(); } addRecordsToStream(records); Map<TopicPartition, OffsetAndMetadata> offsetData = this.acknowlegmentHandler.getAcknowledged(); commitOffsets(offsetData); } } catch (WakeupException e) { log.info("Wakeup, controlLoop exiting"); } catch (InterruptedException e) { log.info("Interrupted, controlLoop exiting"); } catch (Throwable t) { log.error("controlLoop error, exiting", t); throw t; } finally { running = false; try { Map<TopicPartition, OffsetAndMetadata> offsetData = this.acknowlegmentHandler.removePartitions(kafkaConsumer.assignment()); try { if (failedOffset != null) { log.debug("failedOffset of size: " + failedOffset.size()); for (Map.Entry<TopicPartition, OffsetAndMetadata> entry : failedOffset.entrySet()) { offsetData.merge(entry.getKey(), entry.getValue(), (oldO, newO) -> { if (oldO == null) { return newO; } if (newO == null) { return oldO; } return newO.offset() > oldO.offset() ? newO : oldO; }); } } log.info("closing offset size committed: " + offsetData.size()); try { kafkaConsumer.commitSync(offsetData); } catch (WakeupException wakeup) { log.info("controlLoop wakeup on closing commitSync, retrying"); kafkaConsumer.commitSync(offsetData); } } catch (Throwable e) { log.error("controlLoop error commiting sync", e); } } catch (InterruptedException e) { log.info("controlLoop failed to commit offsets on shutdown", e); } log.info("controlLoop closing kafkaConsumer"); kafkaConsumer.close(); } }

Target:
private void controlLoop() { try { while (running) { boolean updatedAssignment = false; if (topics.size() == 0) { commitPendingAndWaitForTopics(); } if (topicsChanged) { commitPendingOffsetsForRemovedTopics(); kafkaConsumer.subscribe(topics, new RebalanceListener()); updatedAssignment = true; } ConsumerRecords<?, ?> records = flowControlledPoll(); if (updatedAssignment) { updateAssignment(); } addRecordsToStream(records); Map<TopicPartition, OffsetAndMetadata> offsets = acknowledgmentHandler.getAcknowledged(); commitOffsets(offsets); } } catch (WakeupException e) { log.info("Wakeup, controlLoop exiting"); } catch (InterruptedException e) { log.info("Interrupted, controlLoop exiting"); } catch (Throwable t) { log.error("controlLoop error, exiting", t); throw t; } finally { running = false; try { Map<TopicPartition, OffsetAndMetadata> finalOffsets = new HashMap<>(); if (failedOffset != null) { finalOffsets.putAll(failedOffset); } finalOffsets.putAll(acknowledgmentHandler.removePartitions(kafkaConsumer.assignment())); try { log.info("closing offset size committed: " + finalOffsets.size()); try { kafkaConsumer.commitSync(finalOffsets); } catch (WakeupException expected) { log.info("controlLoop wakeup on closing commitSync, retrying"); kafkaConsumer.commitSync(finalOffsets); } } catch (Throwable e) { log.error("controlLoop error commiting sync", e); } } catch (InterruptedException e) { log.info("controlLoop failed to commit offsets on shutdown", e); } log.info("controlLoop closing kafkaConsumer"); kafkaConsumer.close(); } }