refactor: RebalanceListener als eigenständige Klasse
authorKai Moritz <kai@juplo.de>
Fri, 12 Aug 2022 09:13:54 +0000 (11:13 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 12 Aug 2022 14:45:21 +0000 (16:45 +0200)
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/EndlessConsumer.java
src/main/java/de/juplo/kafka/WordcountRebalanceListener.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/ApplicationTests.java

index b077a90..da1605b 100644 (file)
@@ -32,10 +32,18 @@ public class ApplicationConfiguration
         consumer);
   }
 
+  @Bean
+  public WordcountRebalanceListener wordcountRebalanceListener(
+      WordcountRecordHandler wordcountRecordHandler)
+  {
+    return new WordcountRebalanceListener(wordcountRecordHandler);
+  }
+
   @Bean
   public EndlessConsumer<String, String> endlessConsumer(
       KafkaConsumer<String, String> kafkaConsumer,
       ExecutorService executor,
+      WordcountRebalanceListener wordcountRebalanceListener,
       WordcountRecordHandler wordcountRecordHandler,
       ApplicationProperties properties)
   {
@@ -45,6 +53,7 @@ public class ApplicationConfiguration
             properties.getClientId(),
             properties.getTopic(),
             kafkaConsumer,
+            wordcountRebalanceListener,
             wordcountRecordHandler);
   }
 
index 0c107f3..0f3316d 100644 (file)
@@ -19,12 +19,13 @@ import java.util.concurrent.locks.ReentrantLock;
 
 @Slf4j
 @RequiredArgsConstructor
-public class EndlessConsumer<K, V> implements ConsumerRebalanceListener, Runnable
+public class EndlessConsumer<K, V> implements Runnable
 {
   private final ExecutorService executor;
   private final String id;
   private final String topic;
   private final Consumer<K, V> consumer;
+  private final ConsumerRebalanceListener rebalanceListener;
   private final RecordHandler<K, V> handler;
 
   private final Lock lock = new ReentrantLock();
@@ -34,18 +35,6 @@ public class EndlessConsumer<K, V> implements ConsumerRebalanceListener, Runnabl
   private long consumed = 0;
 
 
-  @Override
-  public void onPartitionsRevoked(Collection<TopicPartition> partitions)
-  {
-    partitions.forEach(tp -> handler.onPartitionRevoked(tp));
-  }
-
-  @Override
-  public void onPartitionsAssigned(Collection<TopicPartition> partitions)
-  {
-    partitions.forEach(tp -> handler.onPartitionAssigned(tp));
-  }
-
 
   @Override
   public void run()
@@ -53,7 +42,7 @@ public class EndlessConsumer<K, V> implements ConsumerRebalanceListener, Runnabl
     try
     {
       log.info("{} - Subscribing to topic {}", id, topic);
-      consumer.subscribe(Arrays.asList(topic), this);
+      consumer.subscribe(Arrays.asList(topic), rebalanceListener);
 
       while (true)
       {
diff --git a/src/main/java/de/juplo/kafka/WordcountRebalanceListener.java b/src/main/java/de/juplo/kafka/WordcountRebalanceListener.java
new file mode 100644 (file)
index 0000000..fd551c2
--- /dev/null
@@ -0,0 +1,27 @@
+package de.juplo.kafka;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
+import org.apache.kafka.common.TopicPartition;
+
+import java.util.Collection;
+
+
+@RequiredArgsConstructor
+public class WordcountRebalanceListener implements ConsumerRebalanceListener
+{
+  private final RecordHandler<String, String> handler;
+
+
+  @Override
+  public void onPartitionsAssigned(Collection<TopicPartition> partitions)
+  {
+    partitions.forEach(tp -> handler.onPartitionAssigned(tp));
+  }
+
+  @Override
+  public void onPartitionsRevoked(Collection<TopicPartition> partitions)
+  {
+    partitions.forEach(tp -> handler.onPartitionRevoked(tp));
+  }
+}
index 408a826..f4c2104 100644 (file)
@@ -70,7 +70,9 @@ class ApplicationTests
        ExecutorService executor;
        @Autowired
        PartitionStatisticsRepository repository;
-  @Autowired
+       @Autowired
+       WordcountRebalanceListener wordcountRebalanceListener;
+       @Autowired
        WordcountRecordHandler wordcountRecordHandler;
 
        EndlessConsumer<String, String> endlessConsumer;
@@ -233,6 +235,7 @@ class ApplicationTests
                                                properties.getClientId(),
                                                properties.getTopic(),
                                                kafkaConsumer,
+                                               wordcountRebalanceListener,
                                                captureOffsetAndExecuteTestHandler);
 
                endlessConsumer.start();