X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FApplication.java;h=a9b466da3b0ba3eb8f45aadaf92419a89c7f9833;hb=28861eab2d4da8a0594a115de989ffeb90b05cd4;hp=5226d6bd062bee4003ef4ca00f8f1c37597af3d8;hpb=5a2c467b5b299f975f22d6c0e761686067634adc;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 5226d6b..a9b466d 100644 --- a/src/main/java/de/juplo/kafka/Application.java +++ b/src/main/java/de/juplo/kafka/Application.java @@ -1,52 +1,42 @@ package de.juplo.kafka; -import org.apache.kafka.clients.consumer.KafkaConsumer; -import org.apache.kafka.common.serialization.LongDeserializer; -import org.apache.kafka.common.serialization.StringDeserializer; +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 java.util.Properties; -import java.util.concurrent.Executors; +import javax.annotation.PreDestroy; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Future; @SpringBootApplication -@EnableConfigurationProperties(ApplicationProperties.class) -public class Application +@Slf4j +public class Application implements ApplicationRunner { - @Bean - public EndlessConsumer endlessConsumer( - KafkaConsumer kafkaConsumer, - ApplicationProperties properties) + @Autowired + Executor executor; + @Autowired + Consumer kafkaConsumer; + @Autowired + SimpleConsumer simpleConsumer; + + @Override + public void run(ApplicationArguments args) throws Exception { - EndlessConsumer consumer = - new EndlessConsumer( - Executors.newFixedThreadPool(1), - properties.getClientId(), - properties.getTopic(), - kafkaConsumer); - - consumer.start(); - - return consumer; + log.info("Starting SimpleConsumer"); + executor.execute(simpleConsumer); } - @Bean(destroyMethod = "close") - public KafkaConsumer kafkaConsumer(ApplicationProperties properties) + @PreDestroy + public void shutdown() throws ExecutionException, InterruptedException { - Properties props = new Properties(); - - props.put("bootstrap.servers", properties.getBootstrapServer()); - props.put("group.id", properties.getGroupId()); - props.put("client.id", properties.getClientId()); - props.put("auto.offset.reset", properties.getAutoOffsetReset()); - props.put("metadata.max.age.ms", "1000"); - props.put("key.deserializer", StringDeserializer.class.getName()); - props.put("value.deserializer", LongDeserializer.class.getName()); - - return new KafkaConsumer<>(props); + log.info("Signaling SimpleConsumer to quit its work"); + kafkaConsumer.wakeup(); }