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 ApplicationRecordHandler recordHandler()
22 return new ApplicationRecordHandler();
26 public EndlessConsumer<String, Long> endlessConsumer(
27 KafkaConsumer<String, Long> kafkaConsumer,
28 ExecutorService executor,
29 ApplicationRecordHandler recordHandler,
30 ApplicationProperties properties)
33 new EndlessConsumer<>(
35 properties.getClientId(),
36 properties.getTopic(),
42 public ExecutorService executor()
44 return Executors.newSingleThreadExecutor();
47 @Bean(destroyMethod = "close")
48 public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
50 Properties props = new Properties();
52 props.put("bootstrap.servers", properties.getBootstrapServer());
53 props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
54 props.put("group.id", properties.getGroupId());
55 props.put("client.id", properties.getClientId());
56 props.put("auto.offset.reset", properties.getAutoOffsetReset());
57 props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
58 props.put("metadata.max.age.ms", "1000");
59 props.put("key.deserializer", StringDeserializer.class.getName());
60 props.put("value.deserializer", LongDeserializer.class.getName());
62 return new KafkaConsumer<>(props);