Springify: Konfiguration erfolgt über `KafkaProperties`
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index 76d0c8a..523707f 100644 (file)
@@ -1,33 +1,73 @@
 package de.juplo.kafka;
 
 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.autoconfigure.kafka.KafkaProperties;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.kafka.support.serializer.JsonDeserializer;
 
+import java.util.Optional;
 import java.util.Properties;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
 
 @Configuration
-@EnableConfigurationProperties(ApplicationProperties.class)
+@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
 public class ApplicationConfiguration
 {
   @Bean
-  public EndlessConsumer endlessConsumer(
-      KafkaConsumer<String, String> kafkaConsumer,
+  public ApplicationRecordHandler recordHandler(
+      AdderResults adderResults,
+      KafkaProperties kafkaProperties,
+      ApplicationProperties applicationProperties)
+  {
+    return new ApplicationRecordHandler(
+        adderResults,
+        Optional.ofNullable(applicationProperties.getThrottle()),
+        kafkaProperties.getClientId());
+  }
+
+  @Bean
+  public AdderResults adderResults()
+  {
+    return new AdderResults();
+  }
+
+  @Bean
+  public ApplicationRebalanceListener rebalanceListener(
+      ApplicationRecordHandler recordHandler,
+      AdderResults adderResults,
+      StateRepository stateRepository,
+      KafkaProperties kafkaProperties,
+      ApplicationProperties applicationProperties)
+  {
+    return new ApplicationRebalanceListener(
+        recordHandler,
+        adderResults,
+        stateRepository,
+        kafkaProperties.getClientId());
+  }
+
+  @Bean
+  public EndlessConsumer<String, Message> endlessConsumer(
+      KafkaConsumer<String, Message> kafkaConsumer,
       ExecutorService executor,
-      ApplicationProperties properties)
+      ApplicationRebalanceListener rebalanceListener,
+      ApplicationRecordHandler recordHandler,
+      KafkaProperties kafkaProperties,
+      ApplicationProperties applicationProperties)
   {
     return
-        new EndlessConsumer(
+        new EndlessConsumer<>(
             executor,
-            properties.getClientId(),
-            properties.getTopic(),
-            kafkaConsumer);
+            kafkaProperties.getClientId(),
+            applicationProperties.getTopic(),
+            kafkaConsumer,
+            rebalanceListener,
+            recordHandler);
   }
 
   @Bean
@@ -37,17 +77,23 @@ public class ApplicationConfiguration
   }
 
   @Bean(destroyMethod = "close")
-  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+  public KafkaConsumer<String, Message> kafkaConsumer(KafkaProperties kafkaProperties)
   {
     Properties props = new Properties();
 
-    props.put("bootstrap.servers", properties.getBootstrapServer());
-    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("bootstrap.servers", kafkaProperties.getBootstrapServers());
+    props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.StickyAssignor");
+    props.put("group.id", kafkaProperties.getConsumer().getGroupId());
+    props.put("client.id", kafkaProperties.getClientId());
+    props.put("auto.offset.reset", kafkaProperties.getConsumer().getAutoOffsetReset());
+    props.put("auto.commit.interval.ms", (int)kafkaProperties.getConsumer().getAutoCommitInterval().toMillis());
+    props.put("metadata.max.age.ms", kafkaProperties.getConsumer().getProperties().get("metadata.max.age.ms"));
     props.put("key.deserializer", StringDeserializer.class.getName());
-    props.put("value.deserializer", LongDeserializer.class.getName());
+    props.put("value.deserializer", JsonDeserializer.class.getName());
+    props.put(JsonDeserializer.TRUSTED_PACKAGES, "de.juplo.kafka");
+    props.put(JsonDeserializer.TYPE_MAPPINGS,
+      Message.Type.ADD + ":" + MessageAddNumber.class.getName() + "," +
+      Message.Type.CALC + ":" + MessageCalculateSum.class.getName());
 
     return new KafkaConsumer<>(props);
   }