1 package de.juplo.kafka;
3 import org.apache.kafka.clients.consumer.ConsumerRecord;
4 import org.apache.kafka.clients.consumer.KafkaConsumer;
5 import org.apache.kafka.common.serialization.LongDeserializer;
6 import org.apache.kafka.common.serialization.StringDeserializer;
7 import org.springframework.boot.context.properties.EnableConfigurationProperties;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
11 import java.util.Properties;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.function.Consumer;
18 @EnableConfigurationProperties(ApplicationProperties.class)
19 public class ApplicationConfiguration
22 public Consumer<ConsumerRecord<String, Long>> consumer()
31 public EndlessConsumer<String, Long> endlessConsumer(
32 KafkaConsumer<String, Long> kafkaConsumer,
33 ExecutorService executor,
34 Consumer<ConsumerRecord<String, Long>> handler,
35 ApplicationProperties properties)
38 new EndlessConsumer<>(
40 properties.getClientId(),
41 properties.getTopic(),
47 public ExecutorService executor()
49 return Executors.newSingleThreadExecutor();
52 @Bean(destroyMethod = "close")
53 public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
55 Properties props = new Properties();
57 props.put("bootstrap.servers", properties.getBootstrapServer());
58 props.put("group.id", properties.getGroupId());
59 props.put("client.id", properties.getClientId());
60 props.put("auto.offset.reset", properties.getAutoOffsetReset());
61 props.put("metadata.max.age.ms", "1000");
62 props.put("key.deserializer", StringDeserializer.class.getName());
63 props.put("value.deserializer", LongDeserializer.class.getName());
65 return new KafkaConsumer<>(props);