blob: 4267d8765941e279b86e32ffa8a357d9c2450124 [file] [log] [blame]
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -07001/**
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
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070019import java.util.Properties;
20import java.util.concurrent.Future;
21
Shravan Ambati5a11e172016-07-21 15:55:28 -070022import 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.KafkaProducerService;
30import org.onosproject.kafkaintegration.api.dto.KafkaServerConfig;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070034/**
Shravan Ambati5a11e172016-07-21 15:55:28 -070035 * Implementation of a Kafka Producer.
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070036 */
Shravan Ambati5a11e172016-07-21 15:55:28 -070037@Component
38@Service
39public class Producer implements KafkaProducerService {
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070040 private KafkaProducer<String, byte[]> kafkaProducer = null;
41
42 private final Logger log = LoggerFactory.getLogger(getClass());
43
Shravan Ambati5a11e172016-07-21 15:55:28 -070044 @Activate
45 protected void activate() {
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070046 log.info("Started");
47 }
48
Shravan Ambati5a11e172016-07-21 15:55:28 -070049 @Deactivate
50 protected void deactivate() {
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
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070081 public void stop() {
82 if (kafkaProducer != null) {
83 kafkaProducer.close();
84 kafkaProducer = null;
85 }
86
Shravan Ambati5a11e172016-07-21 15:55:28 -070087 log.info("Kafka Producer has Stopped");
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070088 }
89
Shravan Ambati5a11e172016-07-21 15:55:28 -070090 @Override
91 public void restart(KafkaServerConfig config) {
92 stop();
93 start(config);
94 }
95
96 @Override
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070097 public Future<RecordMetadata> send(ProducerRecord<String, byte[]> record) {
98 return kafkaProducer.send(record);
99 }
100}