`commitAsync()` in `onPartitionsRevoked()`
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
1 package de.juplo.kafka;
2
3 import org.apache.kafka.clients.consumer.KafkaConsumer;
4 import org.apache.kafka.common.serialization.StringDeserializer;
5 import org.springframework.boot.context.properties.EnableConfigurationProperties;
6 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration;
8
9 import java.time.Clock;
10 import java.util.Optional;
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(
22       AdderResults adderResults,
23       ApplicationProperties properties)
24   {
25     return new ApplicationRecordHandler(
26         adderResults,
27         Optional.ofNullable(properties.getThrottle()));
28   }
29
30   @Bean
31   public AdderResults adderResults()
32   {
33     return new AdderResults();
34   }
35
36   @Bean
37   public ApplicationRebalanceListener rebalanceListener(
38       KafkaConsumer<String, String> kafkaConsumer,
39       ApplicationRecordHandler recordHandler,
40       AdderResults adderResults,
41       StateRepository stateRepository,
42       ApplicationProperties properties)
43   {
44     return new ApplicationRebalanceListener(
45         kafkaConsumer,
46         recordHandler,
47         adderResults,
48         stateRepository,
49         properties.getClientId());
50   }
51
52   @Bean
53   public EndlessConsumer<String, String> endlessConsumer(
54       KafkaConsumer<String, String> kafkaConsumer,
55       ExecutorService executor,
56       ApplicationRebalanceListener rebalanceListener,
57       ApplicationRecordHandler recordHandler,
58       ApplicationProperties properties)
59   {
60     return
61         new EndlessConsumer<>(
62             executor,
63             properties.getClientId(),
64             properties.getTopic(),
65             kafkaConsumer,
66             rebalanceListener,
67             recordHandler);
68   }
69
70   @Bean
71   public ExecutorService executor()
72   {
73     return Executors.newSingleThreadExecutor();
74   }
75
76   @Bean(destroyMethod = "close")
77   public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
78   {
79     Properties props = new Properties();
80
81     props.put("bootstrap.servers", properties.getBootstrapServer());
82     props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
83     props.put("group.id", properties.getGroupId());
84     props.put("client.id", properties.getClientId());
85     props.put("auto.offset.reset", properties.getAutoOffsetReset());
86     props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
87     props.put("metadata.max.age.ms", "1000");
88     props.put("key.deserializer", StringDeserializer.class.getName());
89     props.put("value.deserializer", StringDeserializer.class.getName());
90
91     return new KafkaConsumer<>(props);
92   }
93 }