2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.kafka;
23 import java.util.Arrays;
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.messaging.MessagingException;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
33 * The Class KafkaEventSubscriber.
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public class KafkaEventSubscriber implements Runnable {
38 private final String topic;
39 private final String kafkaServerAddress;
40 private long eventsReceivedCount = 0;
42 KafkaConsumer<String, String> consumer;
44 Thread subscriberThread;
47 * Instantiates a new kafka event subscriber.
49 * @param topic the topic
50 * @param kafkaServerAddress the kafka server address
51 * @throws MessagingException the messaging exception
53 public KafkaEventSubscriber(final String topic, final String kafkaServerAddress) throws MessagingException {
55 this.kafkaServerAddress = kafkaServerAddress;
57 final Properties props = new Properties();
58 props.put("bootstrap.servers", kafkaServerAddress);
59 props.put("group.id", "test");
60 props.put("enable.auto.commit", "true");
61 props.put("auto.commit.interval.ms", "1000");
62 props.put("session.timeout.ms", "30000");
63 props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
64 props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
66 consumer = new KafkaConsumer<String, String>(props);
67 consumer.subscribe(Arrays.asList(topic));
69 subscriberThread = new Thread(this);
70 subscriberThread.start();
74 * @see java.lang.Runnable#run()
78 System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": receiving events from Kafka server at "
79 + kafkaServerAddress + " on topic " + topic);
81 while (subscriberThread.isAlive() && !subscriberThread.isInterrupted()) {
83 final ConsumerRecords<String, String> records = consumer.poll(100);
84 for (final ConsumerRecord<String, String> record : records) {
85 System.out.println("******");
86 System.out.println("offset=" + record.offset());
87 System.out.println("key=" + record.key());
88 System.out.println("name=" + record.value());
89 eventsReceivedCount++;
91 } catch (final Exception e) {
97 System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": event reception completed");
101 * Gets the events received count.
103 * @return the events received count
105 public long getEventsReceivedCount() {
106 return eventsReceivedCount;
112 public void shutdown() {
113 subscriberThread.interrupt();
115 while (subscriberThread.isAlive()) {
116 ThreadUtilities.sleep(10);
120 System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": stopped");
127 * @param args the arguments
128 * @throws MessagingException the messaging exception
130 public static void main(final String[] args) throws MessagingException {
131 if (args.length != 2) {
132 System.err.println("usage KafkaEventSubscriber topic kafkaServerAddress");
135 new KafkaEventSubscriber(args[0], args[1]);