blob: 5110001dacaee7d38d2e520d31a9a530c146d10b [file] [log] [blame]
Shravan Ambatia4875d82017-01-09 13:06:51 -08001/**
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Shravan Ambatia4875d82017-01-09 13:06:51 -08003 *
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
Shravan Ambatia4875d82017-01-09 13:06:51 -080019import org.apache.kafka.clients.producer.KafkaProducer;
20import org.apache.kafka.clients.producer.ProducerRecord;
21import org.apache.kafka.clients.producer.RecordMetadata;
Shravan Ambatia4875d82017-01-09 13:06:51 -080022import org.onosproject.kafkaintegration.api.KafkaPublisherAdminService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.onosproject.kafkaintegration.api.KafkaPublisherService;
Shravan Ambatia4875d82017-01-09 13:06:51 -080024import org.onosproject.kafkaintegration.api.dto.KafkaServerConfig;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Deactivate;
Shravan Ambatia4875d82017-01-09 13:06:51 -080028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import java.util.Properties;
32import java.util.concurrent.Future;
33
Shravan Ambatia4875d82017-01-09 13:06:51 -080034/**
35 * Implementation of a Kafka Producer.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Component(service = { KafkaPublisherService.class, KafkaPublisherAdminService.class })
Shravan Ambatia4875d82017-01-09 13:06:51 -080038public class PublishManager implements KafkaPublisherService, KafkaPublisherAdminService {
39 private KafkaProducer<String, byte[]> kafkaProducer = null;
40
41 private final Logger log = LoggerFactory.getLogger(getClass());
42
43 @Activate
44 protected void activate() {
45 log.info("Started");
46 }
47
48 @Deactivate
49 protected void deactivate() {
50 stop();
51 log.info("Stopped");
52 }
53
54 @Override
55 public void start(KafkaServerConfig config) {
56
57 if (kafkaProducer != null) {
58 log.info("Producer has already started");
59 return;
60 }
61
62 String bootstrapServer =
63 new StringBuilder().append(config.getIpAddress()).append(":")
64 .append(config.getPort()).toString();
65
66 // Set Server Properties
67 Properties prop = new Properties();
68 prop.put("bootstrap.servers", bootstrapServer);
69 prop.put("retries", config.getNumOfRetries());
70 prop.put("max.in.flight.requests.per.connection",
71 config.getMaxInFlightRequestsPerConnection());
72 prop.put("request.required.acks", config.getAcksRequired());
73 prop.put("key.serializer", config.getKeySerializer());
74 prop.put("value.serializer", config.getValueSerializer());
75
76 kafkaProducer = new KafkaProducer<>(prop);
77 log.info("Kafka Producer has started.");
78 }
79
80 @Override
81 public void stop() {
82 if (kafkaProducer != null) {
83 kafkaProducer.close();
84 kafkaProducer = null;
85 }
86
87 log.info("Kafka Producer has Stopped");
88 }
89
90 @Override
91 public void restart(KafkaServerConfig config) {
92 stop();
93 start(config);
94 }
95
96 @Override
97 public Future<RecordMetadata> send(ProducerRecord<String, byte[]> record) {
98 return kafkaProducer.send(record);
99 }
100}