Refaktorisierung für Tests - KafkaConsumer als eigenständige Bean
[demos/kafka/training] / src / main / java / de / juplo / kafka / Application.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.SpringApplication;
7 import org.springframework.boot.autoconfigure.SpringBootApplication;
8 import org.springframework.boot.context.properties.EnableConfigurationProperties;
9 import org.springframework.context.annotation.Bean;
10
11 import java.util.Properties;
12 import java.util.concurrent.Executors;
13
14
15 @SpringBootApplication
16 @EnableConfigurationProperties(ApplicationProperties.class)
17 public class Application
18 {
19   @Bean
20   public EndlessConsumer endlessConsumer(
21       KafkaConsumer<String, String> kafkaConsumer,
22       ApplicationProperties properties)
23   {
24     EndlessConsumer consumer =
25         new EndlessConsumer(
26             Executors.newFixedThreadPool(1),
27             properties.getClientId(),
28             properties.getTopic(),
29             kafkaConsumer);
30
31     consumer.start();
32
33     return consumer;
34   }
35
36   @Bean(destroyMethod = "close")
37   public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
38   {
39     Properties props = new Properties();
40
41     props.put("bootstrap.servers", properties.getBootstrapServer());
42     props.put("group.id", properties.getGroupId());
43     props.put("client.id", properties.getClientId());
44     props.put("auto.offset.reset", properties.getAutoOffsetReset());
45     props.put("metadata.max.age.ms", "1000");
46     props.put("key.deserializer", StringDeserializer.class.getName());
47     props.put("value.deserializer", LongDeserializer.class.getName());
48
49     return new KafkaConsumer<>(props);
50   }
51
52
53   public static void main(String[] args)
54   {
55     SpringApplication.run(Application.class, args);
56   }
57 }