54ee9ba926fc340ead1bffa5a05d2ea853b08eb1
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.kafka;
22
23 import com.salesforce.kafka.test.junit4.SharedKafkaTestResource;
24
25 import java.time.Duration;
26 import java.util.Arrays;
27 import java.util.Properties;
28
29 import org.apache.kafka.clients.consumer.ConsumerRecord;
30 import org.apache.kafka.clients.consumer.ConsumerRecords;
31 import org.apache.kafka.clients.consumer.KafkaConsumer;
32 import org.apache.kafka.common.serialization.StringDeserializer;
33 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
34 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
35
36 /**
37  * The Class KafkaEventSubscriber.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class KafkaEventSubscriber implements Runnable {
42     private static final Duration POLL_DURATION = Duration.ofMillis(100);
43
44     private final String topic;
45     private long eventsReceivedCount = 0;
46
47     KafkaConsumer<String, String> consumer;
48
49     Thread subscriberThread;
50
51     /**
52      * Instantiates a new kafka event subscriber.
53      *
54      * @param topic the topic
55      * @param sharedKafkaTestResource the kafka server address
56      * @throws MessagingException the messaging exception
57      */
58     public KafkaEventSubscriber(final String topic, final SharedKafkaTestResource sharedKafkaTestResource)
59                     throws MessagingException {
60         this.topic = topic;
61
62
63         final Properties consumerProperties = new Properties();
64         consumerProperties.put("group.id", "test");
65
66
67         consumer = sharedKafkaTestResource.getKafkaTestUtils().getKafkaConsumer(StringDeserializer.class,
68                         StringDeserializer.class, consumerProperties);
69         consumer.subscribe(Arrays.asList(topic));
70
71         subscriberThread = new Thread(this);
72         subscriberThread.start();
73     }
74
75     /*
76      * (non-Javadoc)
77      * 
78      * @see java.lang.Runnable#run()
79      */
80     @Override
81     public void run() {
82         System.out.println(KafkaEventSubscriber.class.getCanonicalName()
83                         + ": receiving events from Kafka server  on topic " + topic);
84
85         while (subscriberThread.isAlive() && !subscriberThread.isInterrupted()) {
86             try {
87                 final ConsumerRecords<String, String> records = consumer.poll(POLL_DURATION.toMillis());
88                 for (final ConsumerRecord<String, String> record : records) {
89                     eventsReceivedCount++;
90                     System.out.println("****** Received event No. " + eventsReceivedCount + " ******");
91                     System.out.println("offset=" + record.offset());
92                     System.out.println("key=" + record.key());
93                 }
94             } catch (final Exception e) {
95                 // Thread interrupted
96                 break;
97             }
98         }
99
100         System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": event reception completed");
101     }
102
103     /**
104      * Gets the events received count.
105      *
106      * @return the events received count
107      */
108     public long getEventsReceivedCount() {
109         return eventsReceivedCount;
110     }
111
112     /**
113      * Shutdown.
114      */
115     public void shutdown() {
116         subscriberThread.interrupt();
117
118         while (subscriberThread.isAlive()) {
119             ThreadUtilities.sleep(10);
120         }
121
122         consumer.close();
123         System.out.println(KafkaEventSubscriber.class.getCanonicalName() + ": stopped");
124     }
125 }