Auf den `CooperativeStickyAssignor` umgestellt
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index 58f44fa..9b06b09 100644 (file)
@@ -1,5 +1,6 @@
 package de.juplo.kafka;
 
+import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
 import org.apache.kafka.common.serialization.LongDeserializer;
 import org.apache.kafka.common.serialization.StringDeserializer;
@@ -7,8 +8,11 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
+import java.time.Clock;
 import java.util.Properties;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.function.Consumer;
 
 
 @Configuration
@@ -16,26 +20,50 @@ import java.util.concurrent.Executors;
 public class ApplicationConfiguration
 {
   @Bean
-  public EndlessConsumer endlessConsumer(
-      KafkaConsumer<String, String> kafkaConsumer,
+  public Consumer<ConsumerRecord<String, Long>> consumer()
+  {
+    return (record) ->
+    {
+      // Handle record
+    };
+  }
+
+  @Bean
+  public EndlessConsumer<String, Long> endlessConsumer(
+      KafkaConsumer<String, Long> kafkaConsumer,
+      ExecutorService executor,
+      Consumer<ConsumerRecord<String, Long>> handler,
+      PartitionStatisticsRepository repository,
       ApplicationProperties properties)
   {
     return
-        new EndlessConsumer(
-            Executors.newFixedThreadPool(1),
+        new EndlessConsumer<>(
+            executor,
+            repository,
             properties.getClientId(),
             properties.getTopic(),
-            kafkaConsumer);
+            Clock.systemDefaultZone(),
+            properties.getCommitInterval(),
+            kafkaConsumer,
+            handler);
+  }
+
+  @Bean
+  public ExecutorService executor()
+  {
+    return Executors.newSingleThreadExecutor();
   }
 
   @Bean(destroyMethod = "close")
-  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+  public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
   {
     Properties props = new Properties();
 
     props.put("bootstrap.servers", properties.getBootstrapServer());
+    props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
     props.put("group.id", properties.getGroupId());
     props.put("client.id", properties.getClientId());
+    props.put("enable.auto.commit", false);
     props.put("auto.offset.reset", properties.getAutoOffsetReset());
     props.put("metadata.max.age.ms", "1000");
     props.put("key.deserializer", StringDeserializer.class.getName());