Fehler im Shutdown-Code korrigiert: Shutdown von `EndlessConsumer` zu spät
[demos/kafka/training] / src / main / java / de / juplo / kafka / Application.java
index 6601e6d..76c2520 100644 (file)
@@ -8,7 +8,6 @@ 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;
 
@@ -31,8 +30,22 @@ public class Application implements ApplicationRunner
   }
 
   @PreDestroy
-  public void stopExecutor()
+  public void shutdown()
   {
+    try
+    {
+      log.info("Stopping EndlessConsumer");
+      endlessConsumer.stop();
+    }
+    catch (IllegalStateException e)
+    {
+      log.info("Was already stopped: {}", e.toString());
+    }
+    catch (Exception e)
+    {
+      log.error("Unexpected exception while stopping EndlessConsumer: {}", e);
+    }
+
     try
     {
       log.info("Shutting down the ExecutorService.");
@@ -42,16 +55,16 @@ public class Application implements ApplicationRunner
     }
     catch (InterruptedException e)
     {
-      log.error("Exception while waiting for the termination of the ExecutorService: {}", e.toString());
+      log.error("Exception while waiting for the termination of the ExecutorService: {}", e);
     }
     finally
     {
-      if (!executor.isShutdown())
+      if (!executor.isTerminated())
       {
         log.warn("Forcing shutdown of ExecutorService!");
         executor
             .shutdownNow()
-            .forEach(runnable -> log.warn("Unfinished task: {}", runnable.getClass().getSimpleName()));
+            .forEach(runnable -> log.warn("Unprocessed task: {}", runnable.getClass().getSimpleName()));
       }
       log.info("Shutdow of ExecutorService finished");
     }