Refaktorisierung für Tests - Start des EndlessConsumer in ApplicationRunner
[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     return
24         new EndlessConsumer(
25             Executors.newFixedThreadPool(1),
26             properties.getClientId(),
27             properties.getTopic(),
28             kafkaConsumer);
29   }
30
31   @Bean(destroyMethod = "close")
32   public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
33   {
34     Properties props = new Properties();
35
36     props.put("bootstrap.servers", properties.getBootstrapServer());
37     props.put("group.id", properties.getGroupId());
38     props.put("client.id", properties.getClientId());
39     props.put("auto.offset.reset", properties.getAutoOffsetReset());
40     props.put("metadata.max.age.ms", "1000");
41     props.put("key.deserializer", StringDeserializer.class.getName());
42     props.put("value.deserializer", LongDeserializer.class.getName());
43
44     return new KafkaConsumer<>(props);
45   }
46 }