2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.kafka;
23 import org.apache.kafka.clients.CommonClientConfigs;
24 import org.apache.kafka.clients.consumer.Consumer;
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.apache.kafka.common.config.SaslConfigs;
29 import org.apache.kafka.common.security.auth.SecurityProtocol;
30 import org.apache.kafka.common.security.scram.internals.ScramMechanism;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import java.time.Duration;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Properties;
39 public class KafkaConsumerImpl extends KafkaClient {
41 protected static Logger logger = LoggerFactory.getLogger(KafkaConsumerImpl.class);
42 private static final String kafkaBootstrapServers = "kafkaBootstrapServers";
43 private Consumer<String, String> consumer;
45 public KafkaConsumerImpl(String bootstrapServers) throws Exception {
46 super("kafka/default-consumer.properties");
47 setProperties(bootstrapServers);
51 public List<String> get(String topic, String consumerGroup, String consumerId) {
52 logger.info("consuming message from kafka topic : " + topic);
53 this.properties.put("group.id", consumerGroup);
54 this.properties.put("client.id", consumerId);
55 if (consumer == null) {
56 consumer = getKafkaConsumer(properties);
57 consumer.subscribe(Arrays.asList(topic));
59 ArrayList<String> msgs = new ArrayList<>();
60 ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
61 for (ConsumerRecord<String, String> rec : records) {
62 msgs.add(rec.value());
64 logger.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<READING THE CONSUMED MESSAGES<<<<<<<<<<<<<<<<<<<<<<<<<<<");
65 msgs.forEach(msg -> logger.info("MESSAGE CONSUMED FROM KAFKA : <<<<<" + msg + ">>>>>"));
69 private void setProperties(String bootstrapServers) throws Exception {
70 if (bootstrapServers == null) {
71 logger.error("Environment Variable " + kafkaBootstrapServers + " is missing");
72 throw new Exception("Environment Variable " + kafkaBootstrapServers + " is missing");
74 this.properties.put("bootstrap.servers", bootstrapServers);
77 if (System.getenv("JAAS_CONFIG") == null) {
78 logger.info("Not using any authentication for kafka interaction");
80 logger.info("Using {} authentication provided for kafka interaction",
81 ScramMechanism.SCRAM_SHA_512.mechanismName());
82 this.properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, SecurityProtocol.SASL_PLAINTEXT.name);
83 this.properties.put(SaslConfigs.SASL_MECHANISM, ScramMechanism.SCRAM_SHA_512.mechanismName());
84 this.properties.put(SaslConfigs.SASL_JAAS_CONFIG, System.getenv("JAAS_CONFIG"));
88 public static KafkaConsumer<String, String> getKafkaConsumer(Properties properties) {
89 return new KafkaConsumer<>(properties);
92 public void setConsumer(Consumer<String, String> kafkaConsumer) {
93 this.consumer = kafkaConsumer;
97 if (consumer != null) {
98 logger.info("Closing the Kafka Consumer");