WIP:TaskExecutor-NEU
[demos/kafka/training] / src / main / java / de / juplo / kafka / Application.java
index b5bd1b9..a9b466d 100644 (file)
@@ -1,6 +1,7 @@
 package de.juplo.kafka;
 
 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;
@@ -8,9 +9,9 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 import javax.annotation.PreDestroy;
-import java.util.List;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Future;
 
 
 @SpringBootApplication
@@ -18,43 +19,24 @@ import java.util.concurrent.TimeUnit;
 public class Application implements ApplicationRunner
 {
   @Autowired
-  EndlessConsumer endlessConsumer;
+  Executor executor;
   @Autowired
-  ExecutorService executor;
-
+  Consumer<?, ?> kafkaConsumer;
+  @Autowired
+  SimpleConsumer simpleConsumer;
 
   @Override
   public void run(ApplicationArguments args) throws Exception
   {
-    log.info("Starting EndlessConsumer");
-    endlessConsumer.start();
+    log.info("Starting SimpleConsumer");
+    executor.execute(simpleConsumer);
   }
 
   @PreDestroy
-  public void stopExecutor()
+  public void shutdown() throws ExecutionException, InterruptedException
   {
-    try
-    {
-      log.info("Shutting down the ExecutorService.");
-      executor.shutdown();
-      log.info("Waiting 5 seconds for the ExecutorService to terminate...");
-      executor.awaitTermination(5, TimeUnit.SECONDS);
-    }
-    catch (InterruptedException e)
-    {
-      log.error("Exception while waiting for the termination of the ExecutorService: {}", e.toString());
-    }
-    finally
-    {
-      if (!executor.isTerminated())
-      {
-        log.warn("Forcing shutdown of ExecutorService!");
-        executor
-            .shutdownNow()
-            .forEach(runnable -> log.warn("Unprocessed task: {}", runnable.getClass().getSimpleName()));
-      }
-      log.info("Shutdow of ExecutorService finished");
-    }
+    log.info("Signaling SimpleConsumer to quit its work");
+    kafkaConsumer.wakeup();
   }