Open Kilda Java Documentation
MessageProducerConfig.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.config;
17 
20 
21 import org.apache.kafka.clients.producer.ProducerConfig;
22 import org.apache.kafka.common.serialization.StringSerializer;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.context.annotation.PropertySource;
27 import org.springframework.kafka.core.DefaultKafkaProducerFactory;
28 import org.springframework.kafka.core.KafkaTemplate;
29 import org.springframework.kafka.core.ProducerFactory;
30 
31 import java.util.HashMap;
32 import java.util.Map;
33 
37 @Configuration
38 @PropertySource("classpath:northbound.properties")
39 public class MessageProducerConfig {
43  @Value("${kafka.hosts}")
44  private String kafkaHosts;
45 
52  @Bean
53  public Map<String, Object> producerConfigs() {
54  Map<String, Object> props = new HashMap<>();
55  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaHosts);
56  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
57  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
58  props.put(ProducerConfig.RETRIES_CONFIG, 0);
59  props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
60  props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
61  props.put(ProducerConfig.LINGER_MS_CONFIG, 10);
62  props.put(ProducerConfig.ACKS_CONFIG, "all");
63  return props;
64  }
65 
74  @Bean
75  public ProducerFactory<String, String> producerFactory() {
76  return new DefaultKafkaProducerFactory<>(producerConfigs());
77  }
78 
85  @Bean
86  public KafkaTemplate<String, String> kafkaTemplate() {
87  return new KafkaTemplate<>(producerFactory());
88  }
89 
98  @Bean
100  return new KafkaMessageProducer();
101  }
102 }