Vorlage
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index 6e04453..a580eb0 100644 (file)
@@ -1,13 +1,16 @@
 package de.juplo.kafka;
 
 import org.apache.kafka.clients.consumer.KafkaConsumer;
-import org.apache.kafka.common.serialization.LongDeserializer;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.common.serialization.IntegerDeserializer;
 import org.apache.kafka.common.serialization.StringDeserializer;
+import org.apache.kafka.common.serialization.StringSerializer;
 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.JsonSerializer;
 import java.util.Properties;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
 
@@ -16,35 +19,71 @@ import java.util.concurrent.Executors;
 public class ApplicationConfiguration
 {
   @Bean
-  public EndlessConsumer endlessConsumer(
-      KafkaConsumer<String, String> kafkaConsumer,
+  public ApplicationRecordHandler recordHandler(
+      KafkaProducer<String, Object> kafkaProducer,
       ApplicationProperties properties)
   {
-    EndlessConsumer consumer =
-        new EndlessConsumer(
-            Executors.newFixedThreadPool(1),
-            properties.getClientId(),
-            properties.getTopic(),
-            kafkaConsumer);
+    return new ApplicationRecordHandler(
+        kafkaProducer,
+        properties.getClientId(),
+        properties.getTopicOut());
+  }
 
-    consumer.start();
+  @Bean
+  public EndlessConsumer<String, Integer> endlessConsumer(
+      KafkaConsumer<String, Integer> kafkaConsumer,
+      ExecutorService executor,
+      ApplicationRecordHandler recordHandler,
+      ApplicationProperties properties)
+  {
+    return
+        new EndlessConsumer<>(
+            executor,
+            properties.getClientId(),
+            properties.getTopicIn(),
+            kafkaConsumer,
+            recordHandler);
+  }
 
-    return consumer;
+  @Bean
+  public ExecutorService executor()
+  {
+    return Executors.newSingleThreadExecutor();
   }
 
   @Bean(destroyMethod = "close")
-  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+  public KafkaConsumer<String, Integer> 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("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", LongDeserializer.class.getName());
+    props.put("value.deserializer", IntegerDeserializer.class.getName());
 
     return new KafkaConsumer<>(props);
   }
+
+  @Bean(destroyMethod = "close")
+  public KafkaProducer<String, Object> kafkaProducer(ApplicationProperties properties)
+  {
+    Properties props = new Properties();
+    props.put("bootstrap.servers", properties.getBootstrapServer());
+    props.put("client.id", properties.getClientId());
+    props.put("acks", properties.getAcks());
+    props.put("batch.size", properties.getBatchSize());
+    props.put("delivery.timeout.ms", 20000); // 20 Sekunden
+    props.put("request.timeout.ms",  10000); // 10 Sekunden
+    props.put("linger.ms", properties.getLingerMs());
+    props.put("compression.type", properties.getCompressionType());
+    props.put("key.serializer", StringSerializer.class.getName());
+    props.put("value.serializer", "TODO: JsonSerializer konfigurieren");
+
+    return new KafkaProducer<>(props);
+  }
 }