1 package de.juplo.kafka;
3 import org.apache.kafka.clients.consumer.KafkaConsumer;
4 import org.apache.kafka.clients.producer.KafkaProducer;
5 import org.apache.kafka.common.serialization.IntegerDeserializer;
6 import org.apache.kafka.common.serialization.StringDeserializer;
7 import org.apache.kafka.common.serialization.StringSerializer;
8 import org.springframework.boot.context.properties.EnableConfigurationProperties;
9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.kafka.support.serializer.JsonSerializer;
12 import java.util.Properties;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Executors;
18 @EnableConfigurationProperties(ApplicationProperties.class)
19 public class ApplicationConfiguration
22 public ApplicationRecordHandler recordHandler(
23 KafkaProducer<String, Object> kafkaProducer,
24 ApplicationProperties properties)
26 return new ApplicationRecordHandler(
28 properties.getClientId(),
29 properties.getTopicOut());
33 public EndlessConsumer<String, Integer> endlessConsumer(
34 KafkaConsumer<String, Integer> kafkaConsumer,
35 ExecutorService executor,
36 ApplicationRecordHandler recordHandler,
37 ApplicationProperties properties)
40 new EndlessConsumer<>(
42 properties.getClientId(),
43 properties.getTopicIn(),
49 public ExecutorService executor()
51 return Executors.newSingleThreadExecutor();
54 @Bean(destroyMethod = "close")
55 public KafkaConsumer<String, Integer> kafkaConsumer(ApplicationProperties properties)
57 Properties props = new Properties();
59 props.put("bootstrap.servers", properties.getBootstrapServer());
60 props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
61 props.put("group.id", properties.getGroupId());
62 props.put("client.id", properties.getClientId());
63 props.put("auto.offset.reset", properties.getAutoOffsetReset());
64 props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
65 props.put("metadata.max.age.ms", "1000");
66 props.put("key.deserializer", StringDeserializer.class.getName());
67 props.put("value.deserializer", IntegerDeserializer.class.getName());
69 return new KafkaConsumer<>(props);
72 @Bean(destroyMethod = "close")
73 public KafkaProducer<String, Object> kafkaProducer(ApplicationProperties properties)
75 Properties props = new Properties();
76 props.put("bootstrap.servers", properties.getBootstrapServer());
77 props.put("client.id", properties.getClientId());
78 props.put("acks", properties.getAcks());
79 props.put("batch.size", properties.getBatchSize());
80 props.put("delivery.timeout.ms", 20000); // 20 Sekunden
81 props.put("request.timeout.ms", 10000); // 10 Sekunden
82 props.put("linger.ms", properties.getLingerMs());
83 props.put("compression.type", properties.getCompressionType());
84 props.put("key.serializer", StringSerializer.class.getName());
85 props.put("value.serializer", "TODO: JsonSerializer konfigurieren");
87 return new KafkaProducer<>(props);