Merge der Refaktorisierung des EndlessConsumer (Branch 'stored-state')
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
1 package de.juplo.kafka;
2
3 import org.apache.kafka.clients.consumer.ConsumerRecord;
4 import org.apache.kafka.clients.consumer.KafkaConsumer;
5 import org.apache.kafka.common.serialization.LongDeserializer;
6 import org.apache.kafka.common.serialization.StringDeserializer;
7 import org.springframework.boot.context.properties.EnableConfigurationProperties;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
10
11 import java.util.Properties;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.function.Consumer;
15
16
17 @Configuration
18 @EnableConfigurationProperties(ApplicationProperties.class)
19 public class ApplicationConfiguration
20 {
21   @Bean
22   public Consumer<ConsumerRecord<String, Long>> consumer()
23   {
24     return (record) ->
25     {
26       // Handle record
27     };
28   }
29
30   @Bean
31   public EndlessConsumer<String, Long> endlessConsumer(
32       KafkaConsumer<String, Long> kafkaConsumer,
33       ExecutorService executor,
34       Consumer<ConsumerRecord<String, Long>> handler,
35       PartitionStatisticsRepository repository,
36       ApplicationProperties properties)
37   {
38     return
39         new EndlessConsumer<>(
40             executor,
41             repository,
42             properties.getClientId(),
43             properties.getTopic(),
44             kafkaConsumer,
45             handler);
46   }
47
48   @Bean
49   public ExecutorService executor()
50   {
51     return Executors.newSingleThreadExecutor();
52   }
53
54   @Bean(destroyMethod = "close")
55   public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
56   {
57     Properties props = new Properties();
58
59     props.put("bootstrap.servers", properties.getBootstrapServer());
60     props.put("group.id", properties.getGroupId());
61     props.put("client.id", properties.getClientId());
62     props.put("enable.auto.commit", false);
63     props.put("auto.offset.reset", properties.getAutoOffsetReset());
64     props.put("metadata.max.age.ms", "1000");
65     props.put("key.deserializer", StringDeserializer.class.getName());
66     props.put("value.deserializer", LongDeserializer.class.getName());
67
68     return new KafkaConsumer<>(props);
69   }
70 }