1 package de.juplo.kafka;
3 import org.apache.kafka.clients.consumer.KafkaConsumer;
4 import org.apache.kafka.common.serialization.LongDeserializer;
5 import org.apache.kafka.common.serialization.StringDeserializer;
6 import org.springframework.boot.context.properties.EnableConfigurationProperties;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
10 import java.util.Properties;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
16 @EnableConfigurationProperties(ApplicationProperties.class)
17 public class ApplicationConfiguration
20 public KeyCountingRecordHandler keyCountingRecordHandler()
22 return new KeyCountingRecordHandler();
26 public KeyCountingRebalanceListener keyCountingRebalanceListener(
27 KeyCountingRecordHandler keyCountingRecordHandler,
28 ApplicationProperties properties)
30 return new KeyCountingRebalanceListener(
31 keyCountingRecordHandler,
32 properties.getClientId());
36 public EndlessConsumer<String, Long> endlessConsumer(
37 KafkaConsumer<String, Long> kafkaConsumer,
38 ExecutorService executor,
39 KeyCountingRebalanceListener keyCountingRebalanceListener,
40 KeyCountingRecordHandler keyCountingRecordHandler,
41 ApplicationProperties properties)
44 new EndlessConsumer<>(
46 properties.getClientId(),
47 properties.getTopic(),
49 keyCountingRebalanceListener,
50 keyCountingRecordHandler);
54 public ExecutorService executor()
56 return Executors.newSingleThreadExecutor();
59 @Bean(destroyMethod = "close")
60 public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
62 Properties props = new Properties();
64 props.put("bootstrap.servers", properties.getBootstrapServer());
65 props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
66 props.put("group.id", properties.getGroupId());
67 props.put("client.id", properties.getClientId());
68 props.put("auto.offset.reset", properties.getAutoOffsetReset());
69 props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
70 props.put("metadata.max.age.ms", "1000");
71 props.put("key.deserializer", StringDeserializer.class.getName());
72 props.put("value.deserializer", LongDeserializer.class.getName());
74 return new KafkaConsumer<>(props);