Benennung vereinheitlicht und projektunabhängig gemacht
[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.LongDeserializer;
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.util.Properties;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
13
14
15 @Configuration
16 @EnableConfigurationProperties(ApplicationProperties.class)
17 public class ApplicationConfiguration
18 {
19   @Bean
20   public ApplicationRecordHandler recordHandler()
21   {
22     return new ApplicationRecordHandler();
23   }
24
25   @Bean
26   public EndlessConsumer<String, Long> endlessConsumer(
27       KafkaConsumer<String, Long> kafkaConsumer,
28       ExecutorService executor,
29       ApplicationRecordHandler recordHandler,
30       ApplicationProperties properties)
31   {
32     return
33         new EndlessConsumer<>(
34             executor,
35             properties.getClientId(),
36             properties.getTopic(),
37             kafkaConsumer,
38             recordHandler);
39   }
40
41   @Bean
42   public ExecutorService executor()
43   {
44     return Executors.newSingleThreadExecutor();
45   }
46
47   @Bean(destroyMethod = "close")
48   public KafkaConsumer<String, Long> kafkaConsumer(ApplicationProperties properties)
49   {
50     Properties props = new Properties();
51
52     props.put("bootstrap.servers", properties.getBootstrapServer());
53     props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
54     props.put("group.id", properties.getGroupId());
55     props.put("client.id", properties.getClientId());
56     props.put("auto.offset.reset", properties.getAutoOffsetReset());
57     props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
58     props.put("metadata.max.age.ms", "1000");
59     props.put("key.deserializer", StringDeserializer.class.getName());
60     props.put("value.deserializer", LongDeserializer.class.getName());
61
62     return new KafkaConsumer<>(props);
63   }
64 }