WIP:sleep
[demos/kafka/training] / src / main / java / de / juplo / kafka / Application.java
index 94224e1..718676b 100644 (file)
@@ -7,40 +7,45 @@ import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-import org.springframework.kafka.core.ConsumerFactory;
+import org.springframework.scheduling.annotation.EnableAsync;
 
 import javax.annotation.PreDestroy;
+import java.util.concurrent.ExecutionException;
 
 
 @SpringBootApplication
+@EnableAsync
 @Slf4j
 public class Application implements ApplicationRunner
 {
   @Autowired
-  Consumer<String, Message> consumer;
+  Consumer<?, ?> kafkaConsumer;
   @Autowired
   SimpleConsumer simpleConsumer;
 
-
   @Override
   public void run(ApplicationArguments args) throws Exception
   {
-    log.info("Starting EndlessConsumer");
-    simpleConsumer.start();
+    log.info("Starting SimpleConsumer");
+    simpleConsumer.run();
   }
 
   @PreDestroy
-  public void shutdown()
-  {
-    log.info("Signaling the consumer to quit its work");
-    consumer.wakeup();
-  }
-
-  @Bean(destroyMethod = "close")
-  public Consumer<String, Message> kafkaConsumer(ConsumerFactory<String, Message> factory)
+  public void stop() throws ExecutionException, InterruptedException
   {
-    return factory.createConsumer();
+    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");
   }