private void read(final KafkaStream<String, String> stream) { while (stream.iterator().hasNext()) { final int phase = phaser.register(); final MessageAndMetadata<String, String> msg = stream.iterator().next(); final long offset = msg.offset(); final long partition = msg.partition(); unacknowledgedOffsets.add(offset); lastCommittedOffset.compareAndSet(0, offset); currentPartition.compareAndSet(-1, partition); final String jsonString = msg.message(); handler.handle( configuration.getVertxAddress(), jsonString, () -> { unacknowledgedOffsets.remove(offset); phaser.arriveAndDeregister(); }); if (unacknowledgedOffsets.size() >= configuration.getMaxUnacknowledged() || partititionChanged(partition) || tooManyUncommittedOffsets(offset)) { LOG.info( "Got {} unacknowledged messages, waiting for ACKs in order to commit", unacknowledgedOffsets.size()); if (!waitForAcks(phase)) { return; } LOG.info("Continuing message processing"); commitOffsetsIfAllAcknowledged(offset); } } }