WIP:TaskExecutor-NEU
[demos/kafka/training] / src / main / java / de / juplo / kafka / Application.java
index 5226d6b..a9b466d 100644 (file)
@@ -1,52 +1,42 @@
 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 lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.consumer.Consumer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Bean;
 
-import java.util.Properties;
-import java.util.concurrent.Executors;
+import javax.annotation.PreDestroy;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Future;
 
 
 @SpringBootApplication
-@EnableConfigurationProperties(ApplicationProperties.class)
-public class Application
+@Slf4j
+public class Application implements ApplicationRunner
 {
-  @Bean
-  public EndlessConsumer endlessConsumer(
-      KafkaConsumer<String, String> kafkaConsumer,
-      ApplicationProperties properties)
+  @Autowired
+  Executor executor;
+  @Autowired
+  Consumer<?, ?> kafkaConsumer;
+  @Autowired
+  SimpleConsumer simpleConsumer;
+
+  @Override
+  public void run(ApplicationArguments args) throws Exception
   {
-    EndlessConsumer consumer =
-        new EndlessConsumer(
-            Executors.newFixedThreadPool(1),
-            properties.getClientId(),
-            properties.getTopic(),
-            kafkaConsumer);
-
-    consumer.start();
-
-    return consumer;
+    log.info("Starting SimpleConsumer");
+    executor.execute(simpleConsumer);
   }
 
-  @Bean(destroyMethod = "close")
-  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+  @PreDestroy
+  public void shutdown() throws ExecutionException, InterruptedException
   {
-    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 new KafkaConsumer<>(props);
+    log.info("Signaling SimpleConsumer to quit its work");
+    kafkaConsumer.wakeup();
   }