`EndlessConsumer` auf `@KafkaHandler` umgestellt
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index 4473c69..1755747 100644 (file)
@@ -1,79 +1,69 @@
 package de.juplo.kafka;
 
-import org.apache.kafka.clients.consumer.KafkaConsumer;
-import org.apache.kafka.common.serialization.StringDeserializer;
+import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
 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.Optional;
+
+import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
 
 
 @Configuration
-@EnableConfigurationProperties(ApplicationProperties.class)
+@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
 public class ApplicationConfiguration
 {
   @Bean
-  public ApplicationRecordHandler recordHandler()
+  public ApplicationRecordHandler applicationRecordHandler(
+      AdderResults adderResults,
+      KafkaProperties kafkaProperties,
+      ApplicationProperties applicationProperties)
+  {
+    return new ApplicationRecordHandler(
+        adderResults,
+        Optional.ofNullable(applicationProperties.getThrottle()),
+        kafkaProperties.getClientId());
+  }
+
+  @Bean
+  public AdderResults adderResults()
   {
-    return new ApplicationRecordHandler();
+    return new AdderResults();
   }
 
   @Bean
   public ApplicationRebalanceListener rebalanceListener(
       ApplicationRecordHandler recordHandler,
+      AdderResults adderResults,
       StateRepository stateRepository,
-      ApplicationProperties properties)
+      KafkaProperties kafkaProperties)
   {
     return new ApplicationRebalanceListener(
         recordHandler,
+        adderResults,
         stateRepository,
-        properties.getClientId(),
-        Clock.systemDefaultZone(),
-        properties.getCommitInterval());
+        kafkaProperties.getClientId());
   }
 
   @Bean
-  public EndlessConsumer<String, String> endlessConsumer(
-      KafkaConsumer<String, String> kafkaConsumer,
-      ExecutorService executor,
-      ApplicationRebalanceListener rebalanceListener,
-      ApplicationRecordHandler recordHandler,
-      ApplicationProperties properties)
+  public ApplicationErrorHandler applicationErrorHandler()
   {
-    return
-        new EndlessConsumer<>(
-            executor,
-            properties.getClientId(),
-            properties.getTopic(),
-            kafkaConsumer,
-            rebalanceListener,
-            recordHandler);
+    return new ApplicationErrorHandler();
   }
 
   @Bean
-  public ExecutorService executor()
+  public EndlessConsumer endlessConsumer(
+      RecordHandler recordHandler,
+      ApplicationErrorHandler errorHandler,
+      KafkaProperties kafkaProperties,
+      KafkaListenerEndpointRegistry endpointRegistry)
   {
-    return Executors.newSingleThreadExecutor();
-  }
-
-  @Bean(destroyMethod = "close")
-  public KafkaConsumer<String, String> 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("auto.offset.reset", properties.getAutoOffsetReset());
-    props.put("metadata.max.age.ms", "1000");
-    props.put("key.deserializer", StringDeserializer.class.getName());
-    props.put("value.deserializer", StringDeserializer.class.getName());
-
-    return new KafkaConsumer<>(props);
+    return
+        new EndlessConsumer(
+            kafkaProperties.getClientId(),
+            endpointRegistry,
+            errorHandler,
+            recordHandler);
   }
 }