WIP
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index 58f44fa..23e9bec 100644 (file)
@@ -1,46 +1,45 @@
 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.apache.kafka.clients.consumer.Consumer;
+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.util.Properties;
+import org.springframework.kafka.core.ConsumerFactory;
+
+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,
-      ApplicationProperties properties)
+  public SimpleConsumer endlessConsumer(
+      Consumer<String, Message> kafkaConsumer,
+      ExecutorService executor,
+      KafkaProperties kafkaProperties,
+      ApplicationProperties applicationProperties)
   {
     return
-        new EndlessConsumer(
-            Executors.newFixedThreadPool(1),
-            properties.getClientId(),
-            properties.getTopic(),
+        new SimpleConsumer(
+            executor,
+            kafkaProperties.getClientId(),
+            applicationProperties.getTopic(),
             kafkaConsumer);
   }
 
-  @Bean(destroyMethod = "close")
-  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+  @Bean
+  public ExecutorService executor()
   {
-    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("key.deserializer", StringDeserializer.class.getName());
-    props.put("value.deserializer", LongDeserializer.class.getName());
+    return Executors.newSingleThreadExecutor();
+  }
 
-    return new KafkaConsumer<>(props);
+  @Bean(destroyMethod = "close")
+  public Consumer<String, Message> kafkaConsumer(ConsumerFactory<String, Message> factory)
+  {
+    return factory.createConsumer();
   }
 }