Verbesserte Tests und Korrekturen gemerged: sumup-adder -> stored-offsets
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index b077a90..a9d9b15 100644 (file)
@@ -2,6 +2,7 @@ package de.juplo.kafka;
 
 import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.common.serialization.LongDeserializer;
 import org.apache.kafka.common.serialization.StringDeserializer;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
@@ -18,13 +19,21 @@ import java.util.concurrent.Executors;
 public class ApplicationConfiguration
 {
   @Bean
-  public WordcountRecordHandler wordcountRecordHandler(
-      PartitionStatisticsRepository repository,
-      Consumer<String, String> consumer,
+  public ApplicationRecordHandler recordHandler()
+  {
+    return new ApplicationRecordHandler();
+  }
+
+  @Bean
+  public ApplicationRebalanceListener rebalanceListener(
+      ApplicationRecordHandler recordHandler,
+      StateRepository stateRepository,
+      Consumer<String, Long> consumer,
       ApplicationProperties properties)
   {
-    return new WordcountRecordHandler(
-        repository,
+    return new ApplicationRebalanceListener(
+        recordHandler,
+        stateRepository,
         properties.getClientId(),
         properties.getTopic(),
         Clock.systemDefaultZone(),
@@ -33,10 +42,11 @@ public class ApplicationConfiguration
   }
 
   @Bean
-  public EndlessConsumer<String, String> endlessConsumer(
-      KafkaConsumer<String, String> kafkaConsumer,
+  public EndlessConsumer<String, Long> endlessConsumer(
+      KafkaConsumer<String, Long> kafkaConsumer,
       ExecutorService executor,
-      WordcountRecordHandler wordcountRecordHandler,
+      ApplicationRebalanceListener rebalanceListener,
+      ApplicationRecordHandler recordHandler,
       ApplicationProperties properties)
   {
     return
@@ -45,7 +55,8 @@ public class ApplicationConfiguration
             properties.getClientId(),
             properties.getTopic(),
             kafkaConsumer,
-            wordcountRecordHandler);
+            rebalanceListener,
+            recordHandler);
   }
 
   @Bean
@@ -55,7 +66,7 @@ public class ApplicationConfiguration
   }
 
   @Bean(destroyMethod = "close")
-  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+  public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
   {
     Properties props = new Properties();
 
@@ -65,9 +76,10 @@ public class ApplicationConfiguration
     props.put("client.id", properties.getClientId());
     props.put("enable.auto.commit", false);
     props.put("auto.offset.reset", properties.getAutoOffsetReset());
+    props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
     props.put("metadata.max.age.ms", "1000");
     props.put("key.deserializer", StringDeserializer.class.getName());
-    props.put("value.deserializer", StringDeserializer.class.getName());
+    props.put("value.deserializer", LongDeserializer.class.getName());
 
     return new KafkaConsumer<>(props);
   }