blob: f9f49e697a8bb19a66c84c25bf70b5f53bf67b52 [file] [log] [blame]
Shravan Ambatia4875d82017-01-09 13:06:51 -08001/**
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.kafkaintegration.kafka;
18
19import java.util.Properties;
20import java.util.concurrent.Future;
21
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Service;
26import org.apache.kafka.clients.producer.KafkaProducer;
27import org.apache.kafka.clients.producer.ProducerRecord;
28import org.apache.kafka.clients.producer.RecordMetadata;
29import org.onosproject.kafkaintegration.api.KafkaPublisherService;
30import org.onosproject.kafkaintegration.api.KafkaPublisherAdminService;
31import org.onosproject.kafkaintegration.api.dto.KafkaServerConfig;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35/**
36 * Implementation of a Kafka Producer.
37 */
38@Component
39@Service
40public class PublishManager implements KafkaPublisherService, KafkaPublisherAdminService {
41 private KafkaProducer<String, byte[]> kafkaProducer = null;
42
43 private final Logger log = LoggerFactory.getLogger(getClass());
44
45 @Activate
46 protected void activate() {
47 log.info("Started");
48 }
49
50 @Deactivate
51 protected void deactivate() {
52 stop();
53 log.info("Stopped");
54 }
55
56 @Override
57 public void start(KafkaServerConfig config) {
58
59 if (kafkaProducer != null) {
60 log.info("Producer has already started");
61 return;
62 }
63
64 String bootstrapServer =
65 new StringBuilder().append(config.getIpAddress()).append(":")
66 .append(config.getPort()).toString();
67
68 // Set Server Properties
69 Properties prop = new Properties();
70 prop.put("bootstrap.servers", bootstrapServer);
71 prop.put("retries", config.getNumOfRetries());
72 prop.put("max.in.flight.requests.per.connection",
73 config.getMaxInFlightRequestsPerConnection());
74 prop.put("request.required.acks", config.getAcksRequired());
75 prop.put("key.serializer", config.getKeySerializer());
76 prop.put("value.serializer", config.getValueSerializer());
77
78 kafkaProducer = new KafkaProducer<>(prop);
79 log.info("Kafka Producer has started.");
80 }
81
82 @Override
83 public void stop() {
84 if (kafkaProducer != null) {
85 kafkaProducer.close();
86 kafkaProducer = null;
87 }
88
89 log.info("Kafka Producer has Stopped");
90 }
91
92 @Override
93 public void restart(KafkaServerConfig config) {
94 stop();
95 start(config);
96 }
97
98 @Override
99 public Future<RecordMetadata> send(ProducerRecord<String, byte[]> record) {
100 return kafkaProducer.send(record);
101 }
102}