From: Kai Moritz Date: Fri, 4 Nov 2022 10:01:21 +0000 (+0100) Subject: WIP:TaskExecutor-NEU X-Git-Tag: simple-consumer--json-DEPRECATED~4 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=ff0bddc11472768fb63c93f178c1a7b235f13019;p=demos%2Fkafka%2Ftraining WIP:TaskExecutor-NEU --- diff --git a/src/main/java/de/juplo/kafka/Application.java b/src/main/java/de/juplo/kafka/Application.java index a9b466d..705f52f 100644 --- a/src/main/java/de/juplo/kafka/Application.java +++ b/src/main/java/de/juplo/kafka/Application.java @@ -10,7 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PreDestroy; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; @@ -19,17 +19,19 @@ import java.util.concurrent.Future; public class Application implements ApplicationRunner { @Autowired - Executor executor; + ExecutorService executorService; @Autowired Consumer kafkaConsumer; @Autowired SimpleConsumer simpleConsumer; + Future consumerJob; + @Override public void run(ApplicationArguments args) throws Exception { log.info("Starting SimpleConsumer"); - executor.execute(simpleConsumer); + consumerJob = executorService.submit(simpleConsumer); } @PreDestroy @@ -37,6 +39,9 @@ public class Application implements ApplicationRunner { log.info("Signaling SimpleConsumer to quit its work"); kafkaConsumer.wakeup(); + log.info("Waiting for SimpleConsumer to finish its work"); + consumerJob.get(); + log.info("SimpleConsumer finished its work"); } diff --git a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java index de77c60..16926fd 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -8,6 +8,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.kafka.core.ConsumerFactory; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + @Configuration @EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })