X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplication.java;h=b4a960d47e5d8628c52d721d31fd30a53974394b;hb=377840107151d9c270f7e3a91a118dce4aa1295f;hp=7cfe268585f01d11326236e18cfc0b6dcc880176;hpb=32a052b7c494009c59190857984ef3563f4f2b14;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 7cfe268..b4a960d 100644 --- a/src/main/java/de/juplo/kafka/Application.java +++ b/src/main/java/de/juplo/kafka/Application.java @@ -1,53 +1,48 @@ 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.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.util.Assert; +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(); + } - @Bean - public EndlessConsumer consumer() + @PreDestroy + public void shutdown() { - Assert.hasText(properties.getBootstrapServer(), "consumer.bootstrap-server must be set"); - Assert.hasText(properties.getGroupId(), "consumer.group-id must be set"); - Assert.hasText(properties.getClientId(), "consumer.client-id must be set"); - Assert.hasText(properties.getTopic(), "consumer.topic must be set"); - - EndlessConsumer consumer = - new EndlessConsumer( - Executors.newFixedThreadPool(1), - properties.getBootstrapServer(), - properties.getGroupId(), - properties.getClientId(), - properties.getTopic(), - properties.getAutoOffsetReset()); - - consumer.start(); - - return consumer; + log.info("Signaling the consumer to quit its work"); + consumer.wakeup(); } - @Bean - public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() + @Bean(destroyMethod = "close") + public Consumer kafkaConsumer(ConsumerFactory factory) { - return - new Jackson2ObjectMapperBuilder().serializers( - new TopicPartitionSerializer(), - new PartitionStatisticsSerializer()); + return factory.createConsumer(); }