WIP
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
index d9c7661..1d64221 100644 (file)
@@ -1,47 +1,39 @@
 package de.juplo.kafka;
 
 import org.apache.kafka.clients.producer.KafkaProducer;
-import org.apache.kafka.common.serialization.IntegerSerializer;
-import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.kafka.clients.producer.Producer;
+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.annotation.EnableKafka;
+import org.springframework.kafka.core.ProducerFactory;
 
 import java.util.Properties;
 
 
 @Configuration
-@EnableConfigurationProperties(ApplicationProperties.class)
+@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
+@EnableKafka
 public class ApplicationConfiguration
 {
   @Bean
   public RestGateway restGateway(
-      ApplicationProperties properties,
-      KafkaProducer<String, Integer> kafkaProducer)
+      ApplicationProperties applicationProperties,
+      KafkaProperties kafkaProperties,
+      Producer<String, Integer> kafkaProducer)
   {
     return
         new RestGateway(
-            properties.getClientId(),
-            properties.getTopic(),
-            properties.getPartition(),
+            kafkaProperties.getClientId(),
+            applicationProperties.getTopic(),
+            applicationProperties.getPartition(),
             kafkaProducer);
   }
 
   @Bean(destroyMethod = "close")
-  public KafkaProducer<String, Integer> kafkaProducer(ApplicationProperties properties)
+  public Producer<String, Integer> kafkaProducer(ProducerFactory<String, Integer> factory)
   {
-    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", IntegerSerializer.class.getName());
-
-    return new KafkaProducer<>(props);
+    return factory.createProducer();
   }
 }