2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-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.consumer.ConsumerRecord;
26 import org.apache.kafka.clients.consumer.ConsumerRecords;
27 import org.apache.kafka.clients.consumer.KafkaConsumer;
28 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
31 import org.onap.policy.apex.service.engine.event.ApexPluginsEventConsumer;
32 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * This class implements an Apex event consumer that receives events using Kafka.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class ApexKafkaConsumer extends ApexPluginsEventConsumer {
42 // Get a reference to the logger
43 private static final Logger LOGGER = LoggerFactory.getLogger(ApexKafkaConsumer.class);
45 // The Kafka parameters read from the parameter service
46 private KafkaCarrierTechnologyParameters kafkaConsumerProperties;
48 // The event receiver that will receive events from this consumer
49 private ApexEventReceiver eventReceiver;
55 public void init(final String consumerName, final EventHandlerParameters consumerParameters,
56 final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
57 this.eventReceiver = incomingEventReceiver;
58 this.name = consumerName;
60 // Check and get the Kafka Properties
61 if (!(consumerParameters.getCarrierTechnologyParameters() instanceof KafkaCarrierTechnologyParameters)) {
62 throw new ApexEventException("specified consumer properties of type \""
63 + consumerParameters.getCarrierTechnologyParameters().getClass().getName()
64 + "\" are not applicable to a Kafka consumer");
66 kafkaConsumerProperties =
67 (KafkaCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters();
75 // Kick off the Kafka consumer
76 try (KafkaConsumer<String, String> kafkaConsumer =
77 new KafkaConsumer<>(kafkaConsumerProperties.getKafkaConsumerProperties())) {
78 kafkaConsumer.subscribe(kafkaConsumerProperties.getConsumerTopicListAsCollection());
79 if (LOGGER.isDebugEnabled()) {
80 LOGGER.debug("event receiver for {}:{} subscribed to topics: {}", this.getClass().getName(), this.name,
81 kafkaConsumerProperties.getConsumerTopicList());
84 // The endless loop that receives events over Kafka
85 while (consumerThread.isAlive() && !stopOrderedFlag) {
87 final ConsumerRecords<String, String> records =
88 kafkaConsumer.poll(kafkaConsumerProperties.getConsumerPollDuration());
89 for (final ConsumerRecord<String, String> record : records) {
90 traceIfTraceEnabled(record);
91 eventReceiver.receiveEvent(new Properties(), record.value());
93 } catch (final Exception e) {
94 LOGGER.warn("error receiving events on thread {}", consumerThread.getName(), e);
101 * Trace a record if trace is enabled.
103 * @param record the record to trace
105 private void traceIfTraceEnabled(final ConsumerRecord<String, String> record) {
106 if (LOGGER.isTraceEnabled()) {
107 LOGGER.trace("event received for {} for forwarding to Apex engine : {} {}",
108 this.getClass().getName() + ":" + this.name, record.key(), record.value());
117 stopOrderedFlag = true;
119 while (consumerThread.isAlive()) {
120 ThreadUtilities.sleep(kafkaConsumerProperties.getConsumerPollTime());