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 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 19 | import org.apache.kafka.clients.producer.KafkaProducer; |
| 20 | import org.apache.kafka.clients.producer.ProducerRecord; |
| 21 | import org.apache.kafka.clients.producer.RecordMetadata; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 22 | import org.onosproject.kafkaintegration.api.KafkaPublisherAdminService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 23 | import org.onosproject.kafkaintegration.api.KafkaPublisherService; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 24 | import org.onosproject.kafkaintegration.api.dto.KafkaServerConfig; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import java.util.Properties; |
| 32 | import java.util.concurrent.Future; |
| 33 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Implementation of a Kafka Producer. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Component(service = { KafkaPublisherService.class, KafkaPublisherAdminService.class }) |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 38 | public 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 | } |