ROT: Merge der korrigierten Test-Logik deserialization -> into sumup-adder
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
1 package de.juplo.kafka;
2
3 import org.apache.kafka.clients.consumer.Consumer;
4 import org.apache.kafka.clients.consumer.KafkaConsumer;
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;
9
10 import java.time.Clock;
11 import java.util.Properties;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14
15
16 @Configuration
17 @EnableConfigurationProperties(ApplicationProperties.class)
18 public class ApplicationConfiguration
19 {
20   @Bean
21   public ApplicationRecordHandler recordHandler(AdderResults adderResults)
22   {
23     return new ApplicationRecordHandler(adderResults);
24   }
25
26   @Bean
27   public AdderResults adderResults()
28   {
29     return new AdderResults();
30   }
31
32   @Bean
33   public ApplicationRebalanceListener rebalanceListener(
34       ApplicationRecordHandler recordHandler,
35       AdderResults adderResults,
36       StateRepository stateRepository,
37       Consumer<String, String> consumer,
38       ApplicationProperties properties)
39   {
40     return new ApplicationRebalanceListener(
41         recordHandler,
42         adderResults,
43         stateRepository,
44         properties.getClientId(),
45         properties.getTopic(),
46         Clock.systemDefaultZone(),
47         properties.getCommitInterval(),
48         consumer);
49   }
50
51   @Bean
52   public EndlessConsumer<String, String> endlessConsumer(
53       KafkaConsumer<String, String> kafkaConsumer,
54       ExecutorService executor,
55       ApplicationRebalanceListener rebalanceListener,
56       ApplicationRecordHandler recordHandler,
57       ApplicationProperties properties)
58   {
59     return
60         new EndlessConsumer<>(
61             executor,
62             properties.getClientId(),
63             properties.getTopic(),
64             kafkaConsumer,
65             rebalanceListener,
66             recordHandler);
67   }
68
69   @Bean
70   public ExecutorService executor()
71   {
72     return Executors.newSingleThreadExecutor();
73   }
74
75   @Bean(destroyMethod = "close")
76   public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
77   {
78     Properties props = new Properties();
79
80     props.put("bootstrap.servers", properties.getBootstrapServer());
81     props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
82     props.put("group.id", properties.getGroupId());
83     props.put("client.id", properties.getClientId());
84     props.put("enable.auto.commit", false);
85     props.put("auto.offset.reset", properties.getAutoOffsetReset());
86     props.put("metadata.max.age.ms", "1000");
87     props.put("key.deserializer", StringDeserializer.class.getName());
88     props.put("value.deserializer", StringDeserializer.class.getName());
89
90     return new KafkaConsumer<>(props);
91   }
92 }