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 PartitionStatisticsRepository repository,
36 ApplicationProperties properties)
39 new EndlessConsumer<>(
42 properties.getClientId(),
43 properties.getTopic(),
49 public ExecutorService executor()
51 return Executors.newSingleThreadExecutor();
54 @Bean(destroyMethod = "close")
55 public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
57 Properties props = new Properties();
59 props.put("bootstrap.servers", properties.getBootstrapServer());
60 props.put("group.id", properties.getGroupId());
61 props.put("client.id", properties.getClientId());
62 props.put("enable.auto.commit", false);
63 props.put("auto.offset.reset", properties.getAutoOffsetReset());
64 props.put("metadata.max.age.ms", "1000");
65 props.put("key.deserializer", StringDeserializer.class.getName());
66 props.put("value.deserializer", LongDeserializer.class.getName());
68 return new KafkaConsumer<>(props);