Vorlage für die JSON-Version des Rest-Producers
[demos/kafka/training] / src / main / java / de / juplo / kafka / ApplicationConfiguration.java
1 package de.juplo.kafka;
2
3 import org.apache.kafka.clients.producer.KafkaProducer;
4 import org.apache.kafka.common.serialization.IntegerSerializer;
5 import org.apache.kafka.common.serialization.StringSerializer;
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
12
13 @Configuration
14 @EnableConfigurationProperties(ApplicationProperties.class)
15 public class ApplicationConfiguration
16 {
17   @Bean
18   public RestProducer restProducer(
19       ApplicationProperties properties,
20       KafkaProducer<String, Object> kafkaProducer)
21   {
22     return
23         new RestProducer(
24             properties.getClientId(),
25             properties.getTopic(),
26             properties.getPartition(),
27             kafkaProducer);
28   }
29
30   @Bean(destroyMethod = "close")
31   public KafkaProducer<String, Object> kafkaProducer(ApplicationProperties properties)
32   {
33     Properties props = new Properties();
34     props.put("bootstrap.servers", properties.getBootstrapServer());
35     props.put("client.id", properties.getClientId());
36     props.put("acks", properties.getAcks());
37     props.put("batch.size", properties.getBatchSize());
38     props.put("delivery.timeout.ms", 20000); // 20 Sekunden
39     props.put("request.timeout.ms",  10000); // 10 Sekunden
40     props.put("linger.ms", properties.getLingerMs());
41     props.put("compression.type", properties.getCompressionType());
42     props.put("key.serializer", StringSerializer.class.getName());
43     props.put("value.serializer", IntegerSerializer.class.getName());
44
45     return new KafkaProducer<>(props);
46   }
47 }