WIP:sleep
[demos/kafka/training] / src / main / java / de / juplo / kafka / Application.java
index f227bbe..718676b 100644 (file)
@@ -1,26 +1,51 @@
 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;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableAsync;
+
+import javax.annotation.PreDestroy;
+import java.util.concurrent.ExecutionException;
 
 
 @SpringBootApplication
+@EnableAsync
 @Slf4j
 public class Application implements ApplicationRunner
 {
   @Autowired
-  EndlessConsumer endlessConsumer;
-
+  Consumer<?, ?> kafkaConsumer;
+  @Autowired
+  SimpleConsumer simpleConsumer;
 
   @Override
   public void run(ApplicationArguments args) throws Exception
   {
-    log.info("Starting EndlessConsumer");
-    endlessConsumer.start();
+    log.info("Starting SimpleConsumer");
+    simpleConsumer.run();
+  }
+
+  @PreDestroy
+  public void stop() throws ExecutionException, InterruptedException
+  {
+    log.info("Signaling SimpleConsumer to quit its work");
+    kafkaConsumer.wakeup();
+
+    while (simpleConsumer.isRunning())
+    {
+      log.info("Waiting for SimpleConsumer to finish its work");
+      try
+      {
+        Thread.sleep(1000);
+      }
+      catch (InterruptedException e) {}
+    }
+    log.info("SimpleConsumer finished its work");
   }