X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplication.java;h=b4a960d47e5d8628c52d721d31fd30a53974394b;hb=377840107151d9c270f7e3a91a118dce4aa1295f;hp=de4b66d74cdbe0393dddee826fb05201032c1ff1;hpb=7a41d0ba78a49b271962549015c1f867d7055030;p=demos%2Fkafka%2Ftraining diff --git a/src/main/java/de/juplo/kafka/Application.java b/src/main/java/de/juplo/kafka/Application.java index de4b66d..b4a960d 100644 --- a/src/main/java/de/juplo/kafka/Application.java +++ b/src/main/java/de/juplo/kafka/Application.java @@ -1,39 +1,51 @@ package de.juplo.kafka; +import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.consumer.Consumer; 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; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.kafka.core.ConsumerFactory; -import java.util.concurrent.Executors; +import javax.annotation.PreDestroy; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; @SpringBootApplication -@EnableConfigurationProperties(ApplicationProperties.class) -public class Application +@Slf4j +public class Application implements ApplicationRunner { @Autowired - ApplicationProperties properties; + Consumer consumer; + @Autowired + SimpleConsumer simpleConsumer; + + + @Override + public void run(ApplicationArguments args) throws Exception + { + log.info("Starting EndlessConsumer"); + simpleConsumer.start(); + } + @PreDestroy + public void shutdown() + { + log.info("Signaling the consumer to quit its work"); + consumer.wakeup(); + } - @Bean - public EndlessConsumer consumer() + @Bean(destroyMethod = "close") + public Consumer kafkaConsumer(ConsumerFactory factory) { - EndlessConsumer consumer = - new EndlessConsumer( - Executors.newFixedThreadPool(1), - properties.getBootstrapServer(), - properties.getGroupId(), - properties.getClientId(), - properties.getTopic(), - properties.getAutoOffsetReset()); - - consumer.start(); - - return consumer; + return factory.createConsumer(); } + public static void main(String[] args) { SpringApplication.run(Application.class, args);