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;
26 import org.apache.kafka.clients.producer.KafkaProducer;
27 import org.apache.kafka.clients.producer.Producer;
28 import org.apache.kafka.clients.producer.ProducerRecord;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
31 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Concrete implementation of an Apex event producer that sends events using Kafka.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class ApexKafkaProducer extends ApexPluginsEventProducer {
42 // Get a reference to the logger
43 private static final Logger LOGGER = LoggerFactory.getLogger(ApexKafkaProducer.class);
45 // The Kafka parameters read from the parameter service
46 private KafkaCarrierTechnologyParameters kafkaProducerProperties;
48 // The Kafka Producer used to send events using Kafka
49 private Producer<String, Object> kafkaProducer;
52 public void init(final String producerName, final EventHandlerParameters producerParameters)
53 throws ApexEventException {
54 this.name = producerName;
56 // Check and get the Kafka Properties
57 if (!(producerParameters.getCarrierTechnologyParameters() instanceof KafkaCarrierTechnologyParameters)) {
58 String message = "specified producer properties are not applicable to a Kafka producer (" + this.name + ")";
60 throw new ApexEventException(message);
62 kafkaProducerProperties =
63 (KafkaCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
70 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
72 super.sendEvent(executionId, executionProperties, eventName, event);
74 // Kafka producer must be started in the same thread as it is stopped, so we must start it here
75 if (kafkaProducer == null) {
76 // Kick off the Kafka producer
77 kafkaProducer = new KafkaProducer<>(kafkaProducerProperties.getKafkaProducerProperties());
78 if (LOGGER.isDebugEnabled()) {
79 LOGGER.debug("event producer {} is ready to send to topics: {}", this.name,
80 kafkaProducerProperties.getProducerTopic());
84 kafkaProducer.send(new ProducerRecord<String, Object>(kafkaProducerProperties.getProducerTopic(), name, event));
85 if (LOGGER.isTraceEnabled()) {
86 LOGGER.trace("event sent from engine using {} to topic {} : {} ", this.name,
87 kafkaProducerProperties.getProducerTopic(), event);
96 if (kafkaProducer != null) {
97 kafkaProducer.flush();
98 kafkaProducer.close();