Umstellung des Nachrichten-Datentyps auf Long zurückgenommen
[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.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 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, String>> consumer()
23   {
24     return (record) ->
25     {
26       // Handle record
27     };
28   }
29
30   @Bean
31   public EndlessConsumer<String, String> endlessConsumer(
32       KafkaConsumer<String, String> kafkaConsumer,
33       ExecutorService executor,
34       Consumer<ConsumerRecord<String, String>> handler,
35       PartitionStatisticsRepository repository,
36       ApplicationProperties properties)
37   {
38     return
39         new EndlessConsumer<>(
40             executor,
41             repository,
42             properties.getClientId(),
43             properties.getTopic(),
44             Clock.systemDefaultZone(),
45             properties.getCommitInterval(),
46             kafkaConsumer,
47             handler);
48   }
49
50   @Bean
51   public ExecutorService executor()
52   {
53     return Executors.newSingleThreadExecutor();
54   }
55
56   @Bean(destroyMethod = "close")
57   public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
58   {
59     Properties props = new Properties();
60
61     props.put("bootstrap.servers", properties.getBootstrapServer());
62     props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
63     props.put("group.id", properties.getGroupId());
64     props.put("client.id", properties.getClientId());
65     props.put("enable.auto.commit", false);
66     props.put("auto.offset.reset", properties.getAutoOffsetReset());
67     props.put("metadata.max.age.ms", "1000");
68     props.put("key.deserializer", StringDeserializer.class.getName());
69     props.put("value.deserializer", StringDeserializer.class.getName());
70
71     return new KafkaConsumer<>(props);
72   }
73 }