Refaktorisierung für Tests - ExecutorService als separate Bean erzeugt
[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 EndlessConsumer endlessConsumer(
21       KafkaConsumer<String, String> kafkaConsumer,
22       ExecutorService executor,
23       ApplicationProperties properties)
24   {
25     return
26         new EndlessConsumer(
27             executor,
28             properties.getClientId(),
29             properties.getTopic(),
30             kafkaConsumer);
31   }
32
33   @Bean
34   public ExecutorService executor()
35   {
36     return Executors.newSingleThreadExecutor();
37   }
38
39   @Bean(destroyMethod = "close")
40   public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
41   {
42     Properties props = new Properties();
43
44     props.put("bootstrap.servers", properties.getBootstrapServer());
45     props.put("group.id", properties.getGroupId());
46     props.put("client.id", properties.getClientId());
47     props.put("auto.offset.reset", properties.getAutoOffsetReset());
48     props.put("metadata.max.age.ms", "1000");
49     props.put("key.deserializer", StringDeserializer.class.getName());
50     props.put("value.deserializer", LongDeserializer.class.getName());
51
52     return new KafkaConsumer<>(props);
53   }
54 }