ROT: Verbesserungen aus 'deserialization' in 'sumup-adder' gemerged
[demos/kafka/training] / src / main / java / de / juplo / kafka / Application.java
1 package de.juplo.kafka;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.boot.ApplicationArguments;
6 import org.springframework.boot.ApplicationRunner;
7 import org.springframework.boot.SpringApplication;
8 import org.springframework.boot.autoconfigure.SpringBootApplication;
9
10 import javax.annotation.PreDestroy;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.TimeUnit;
13
14
15 @SpringBootApplication
16 @Slf4j
17 public class Application implements ApplicationRunner
18 {
19   @Autowired
20   EndlessConsumer endlessConsumer;
21   @Autowired
22   ExecutorService executor;
23
24
25   @Override
26   public void run(ApplicationArguments args) throws Exception
27   {
28     log.info("Starting EndlessConsumer");
29     endlessConsumer.start();
30   }
31
32   @PreDestroy
33   public void shutdown()
34   {
35     try
36     {
37       log.info("Stopping EndlessConsumer");
38       endlessConsumer.stop();
39     }
40     catch (IllegalStateException e)
41     {
42       log.info("Was already stopped: {}", e.toString());
43     }
44     catch (Exception e)
45     {
46       log.error("Unexpected exception while stopping EndlessConsumer: {}", e);
47     }
48
49     try
50     {
51       log.info("Shutting down the ExecutorService.");
52       executor.shutdown();
53       log.info("Waiting 5 seconds for the ExecutorService to terminate...");
54       executor.awaitTermination(5, TimeUnit.SECONDS);
55     }
56     catch (InterruptedException e)
57     {
58       log.error("Exception while waiting for the termination of the ExecutorService: {}", e);
59     }
60     finally
61     {
62       if (!executor.isTerminated())
63       {
64         log.warn("Forcing shutdown of ExecutorService!");
65         executor
66             .shutdownNow()
67             .forEach(runnable -> log.warn("Unprocessed task: {}", runnable.getClass().getSimpleName()));
68       }
69       log.info("Shutdow of ExecutorService finished");
70     }
71   }
72
73
74   public static void main(String[] args)
75   {
76     SpringApplication.run(Application.class, args);
77   }
78 }