Refaktorisierung für Tests - Bean-Definition in Config-Klasse verschoben
[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.Executors;
12
13
14 @Configuration
15 @EnableConfigurationProperties(ApplicationProperties.class)
16 public class ApplicationConfiguration
17 {
18   @Bean
19   public EndlessConsumer endlessConsumer(
20       KafkaConsumer<String, String> kafkaConsumer,
21       ApplicationProperties properties)
22   {
23     EndlessConsumer consumer =
24         new EndlessConsumer(
25             Executors.newFixedThreadPool(1),
26             properties.getClientId(),
27             properties.getTopic(),
28             kafkaConsumer);
29
30     consumer.start();
31
32     return consumer;
33   }
34
35   @Bean(destroyMethod = "close")
36   public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
37   {
38     Properties props = new Properties();
39
40     props.put("bootstrap.servers", properties.getBootstrapServer());
41     props.put("group.id", properties.getGroupId());
42     props.put("client.id", properties.getClientId());
43     props.put("auto.offset.reset", properties.getAutoOffsetReset());
44     props.put("metadata.max.age.ms", "1000");
45     props.put("key.deserializer", StringDeserializer.class.getName());
46     props.put("value.deserializer", LongDeserializer.class.getName());
47
48     return new KafkaConsumer<>(props);
49   }
50 }