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.apps.uservice.test.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 * @author Liam Fallon (liam.fallon@ericsson.com)
35 public class KafkaEventSubscriber implements Runnable {
36 private final String topic;
37 private final String kafkaServerAddress;
38 private long eventsReceivedCount = 0;
40 KafkaConsumer<String, String> consumer;
42 Thread subscriberThread;
44 public KafkaEventSubscriber(final String topic, final String kafkaServerAddress) throws MessagingException {
46 this.kafkaServerAddress = kafkaServerAddress;
48 final Properties props = new Properties();
49 props.put("bootstrap.servers", kafkaServerAddress);
50 props.put("group.id", "test");
51 props.put("enable.auto.commit", "true");
52 props.put("auto.commit.interval.ms", "1000");
53 props.put("session.timeout.ms", "30000");
54 props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
55 props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
57 consumer = new KafkaConsumer<String, String>(props);
58 consumer.subscribe(Arrays.asList(topic));
60 subscriberThread = new Thread(this);
61 subscriberThread.start();
66 System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": receiving events from Kafka server at "
67 + kafkaServerAddress + " on topic " + topic);
69 while (subscriberThread.isAlive() && !subscriberThread.isInterrupted()) {
71 final ConsumerRecords<String, String> records = consumer.poll(100);
72 for (final ConsumerRecord<String, String> record : records) {
73 System.out.println("******");
74 System.out.println("offset=" + record.offset());
75 System.out.println("key=" + record.key());
76 System.out.println("name=" + record.value());
77 eventsReceivedCount++;
79 } catch (final Exception e) {
85 System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": event reception completed");
88 public long getEventsReceivedCount() {
89 return eventsReceivedCount;
92 public void shutdown() {
93 subscriberThread.interrupt();
95 while (subscriberThread.isAlive()) {
96 ThreadUtilities.sleep(10);
100 System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": stopped");
104 public static void main(final String[] args) throws MessagingException {
105 if (args.length != 2) {
106 System.err.println("usage KafkaEventSubscriber topic kafkaServerAddress");
109 new KafkaEventSubscriber(args[0], args[1]);