From b9ca34cf63146e9ae5e72a8722a92f17615586d2 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 9 Apr 2022 11:36:29 +0200 Subject: [PATCH] =?utf8?q?Refaktorisierung=20f=C3=BCr=20Tests=20-=20Start?= =?utf8?q?=20des=20EndlessConsumer=20in=20ApplicationRunner?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Bisher wurde der EndlessConsumer beim erzeugen der Bean gestartet * Dies ist für das Aufsetzen von Tests ungünstig, da die erzeugte Bean dort nicht unbedingt direkt gestartet werden soll * Daher wurde der Start des EndlessConsumer in einen ApplicationRunner ausgelagert --- src/main/java/de/juplo/kafka/Application.java | 19 ++++++++++++++++++- .../juplo/kafka/ApplicationConfiguration.java | 6 +----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/juplo/kafka/Application.java b/src/main/java/de/juplo/kafka/Application.java index 0069257..f227bbe 100644 --- a/src/main/java/de/juplo/kafka/Application.java +++ b/src/main/java/de/juplo/kafka/Application.java @@ -1,12 +1,29 @@ package de.juplo.kafka; +import lombok.extern.slf4j.Slf4j; +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; @SpringBootApplication -public class Application +@Slf4j +public class Application implements ApplicationRunner { + @Autowired + EndlessConsumer endlessConsumer; + + + @Override + public void run(ApplicationArguments args) throws Exception + { + log.info("Starting EndlessConsumer"); + endlessConsumer.start(); + } + + public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java index 6e04453..58f44fa 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -20,16 +20,12 @@ public class ApplicationConfiguration KafkaConsumer kafkaConsumer, ApplicationProperties properties) { - EndlessConsumer consumer = + return new EndlessConsumer( Executors.newFixedThreadPool(1), properties.getClientId(), properties.getTopic(), kafkaConsumer); - - consumer.start(); - - return consumer; } @Bean(destroyMethod = "close") -- 2.20.1