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