@Bean
public ApplicationRebalanceListener rebalanceListener(
+ KafkaConsumer<String, String> kafkaConsumer,
ApplicationRecordHandler recordHandler,
AdderResults adderResults,
StateRepository stateRepository,
ApplicationProperties properties)
{
return new ApplicationRebalanceListener(
+ kafkaConsumer,
recordHandler,
adderResults,
stateRepository,
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.common.TopicPartition;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
+import java.util.concurrent.CountDownLatch;
@RequiredArgsConstructor
@Slf4j
public class ApplicationRebalanceListener implements ConsumerRebalanceListener
{
+ private final Consumer consumer;
private final ApplicationRecordHandler recordHandler;
private final AdderResults adderResults;
private final StateRepository stateRepository;
@Override
public void onPartitionsRevoked(Collection<TopicPartition> partitions)
{
+ log.info("{} - Commiting offsets for all previously assigned partitions", id);
+ CountDownLatch commitDone = new CountDownLatch(1);
+ consumer.commitAsync((offsets, e) ->
+ {
+ commitDone.countDown();
+ if (e == null)
+ {
+ log.error("{} - Could not commit offsets to Kafka!", id, e);
+ }
+ else
+ {
+ offsets.entrySet().stream().forEach(entry ->
+ {
+ log.info("{} - Commited offset for {}: {}", id, entry.getKey(), entry.getValue());
+ });
+ }
+ });
+
partitions.forEach(tp ->
{
Integer partition = tp.partition();
Map<String, List<AdderResult>> results = adderResults.removePartition(partition);
stateRepository.save(new StateDocument(partition, state, results));
});
+
+ try
+ {
+ log.debug("{} - Waiting for async commit to complete", id);
+ commitDone.await();
+ }
+ catch (InterruptedException e)
+ {
+ log.warn(
+ "{} - Interrupted while waiting for async commit in onPartitionsRevoked({})",
+ id,
+ partitions,
+ e);
+ }
}
}