2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.carrier.kafka;
24 import java.util.Properties;
25 import org.apache.kafka.clients.producer.KafkaProducer;
26 import org.apache.kafka.clients.producer.Producer;
27 import org.apache.kafka.clients.producer.ProducerRecord;
28 import org.onap.policy.apex.service.engine.event.ApexEventException;
29 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
30 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Concrete implementation of an Apex event producer that sends events using Kafka.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class ApexKafkaProducer extends ApexPluginsEventProducer {
41 // Get a reference to the logger
42 private static final Logger LOGGER = LoggerFactory.getLogger(ApexKafkaProducer.class);
44 // The Kafka parameters read from the parameter service
45 private KafkaCarrierTechnologyParameters kafkaProducerProperties;
47 // The Kafka Producer used to send events using Kafka
48 private Producer<String, Object> kafkaProducer;
51 public void init(final String producerName, final EventHandlerParameters producerParameters)
52 throws ApexEventException {
53 this.name = producerName;
55 // Check and get the Kafka Properties
56 if (!(producerParameters.getCarrierTechnologyParameters() instanceof KafkaCarrierTechnologyParameters)) {
57 String message = "specified producer properties are not applicable to a Kafka producer (" + this.name + ")";
59 throw new ApexEventException(message);
61 kafkaProducerProperties =
62 (KafkaCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
69 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
71 super.sendEvent(executionId, executionProperties, eventName, event);
73 // Kafka producer must be started in the same thread as it is stopped, so we must start it here
74 if (kafkaProducer == null) {
75 // Kick off the Kafka producer
76 kafkaProducer = new KafkaProducer<>(kafkaProducerProperties.getKafkaProducerProperties());
77 if (LOGGER.isDebugEnabled()) {
78 LOGGER.debug("event producer {} is ready to send to topics: {}", this.name,
79 kafkaProducerProperties.getProducerTopic());
83 kafkaProducer.send(new ProducerRecord<String, Object>(kafkaProducerProperties.getProducerTopic(), name, event));
84 if (LOGGER.isTraceEnabled()) {
85 LOGGER.trace("event sent from engine using {} to topic {} : {} ", this.name,
86 kafkaProducerProperties.getProducerTopic(), event);
95 if (kafkaProducer != null) {
96 kafkaProducer.flush();
97 kafkaProducer.close();