Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 1 | /** |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.kafkaintegration.kafka; |
| 18 | |
| 19 | import java.util.Properties; |
| 20 | import java.util.concurrent.Future; |
| 21 | |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.apache.kafka.clients.producer.KafkaProducer; |
| 27 | import org.apache.kafka.clients.producer.ProducerRecord; |
| 28 | import org.apache.kafka.clients.producer.RecordMetadata; |
| 29 | import org.onosproject.kafkaintegration.api.KafkaPublisherService; |
| 30 | import org.onosproject.kafkaintegration.api.KafkaPublisherAdminService; |
| 31 | import org.onosproject.kafkaintegration.api.dto.KafkaServerConfig; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | /** |
| 36 | * Implementation of a Kafka Producer. |
| 37 | */ |
| 38 | @Component |
| 39 | @Service |
| 40 | public 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 | } |