63da4e69659c470271b522a636165ac5141fc3fb
[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.util.concurrent.TimeUnit;
26
27 import org.apache.kafka.clients.producer.Producer;
28 import org.apache.kafka.clients.producer.ProducerRecord;
29 import org.apache.kafka.common.serialization.StringSerializer;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
32
33 /**
34  * The Class KafkaEventProducer.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class KafkaEventProducer implements Runnable {
39     private final String topic;
40     private final SharedKafkaTestResource sharedKafkaTestResource;
41     private final int eventCount;
42     private final boolean xmlEvents;
43     private final long eventInterval;
44     private long eventsSentCount = 0;
45
46     private final Thread producerThread;
47     private boolean sendEventsFlag = false;
48     private boolean stopFlag = false;
49
50     /**
51      * Instantiates a new kafka event producer.
52      *
53      * @param topic the topic
54      * @param sharedKafkaTestResource the kafka server address
55      * @param eventCount the event count
56      * @param xmlEvents the xml events
57      * @param eventInterval the event interval
58      */
59     public KafkaEventProducer(final String topic, final SharedKafkaTestResource sharedKafkaTestResource,
60                     final int eventCount, final boolean xmlEvents, final long eventInterval) {
61         this.topic = topic;
62         this.sharedKafkaTestResource = sharedKafkaTestResource;
63         this.eventCount = eventCount;
64         this.xmlEvents = xmlEvents;
65         this.eventInterval = eventInterval;
66
67         producerThread = new Thread(this);
68         producerThread.start();
69     }
70
71     /*
72      * (non-Javadoc)
73      * 
74      * @see java.lang.Runnable#run()
75      */
76     @Override
77     public void run() {
78         final Producer<String, String> producer = sharedKafkaTestResource.getKafkaTestUtils()
79                         .getKafkaProducer(StringSerializer.class, StringSerializer.class);
80
81         while (producerThread.isAlive() && !stopFlag) {
82             ThreadUtilities.sleep(50);
83
84             if (sendEventsFlag) {
85                 sendEventsToTopic(producer);
86                 sendEventsFlag = false;
87             }
88         }
89
90         producer.close(1000, TimeUnit.MILLISECONDS);
91     }
92
93     /**
94      * Send events.
95      */
96     public void sendEvents() {
97         sendEventsFlag = true;
98     }
99
100     /**
101      * Send events to topic.
102      *
103      * @param producer the producer
104      */
105     private void sendEventsToTopic(final Producer<String, String> producer) {
106         System.out.println(KafkaEventProducer.class.getCanonicalName()
107                         + ": sending events to Kafka server , event count " + eventCount + ", xmlEvents " + xmlEvents);
108
109         for (int i = 0; i < eventCount; i++) {
110             System.out.println(KafkaEventProducer.class.getCanonicalName() + ": waiting " + eventInterval
111                             + " milliseconds before sending next event");
112             ThreadUtilities.sleep(eventInterval);
113
114             String eventString = null;
115             if (xmlEvents) {
116                 eventString = EventGenerator.xmlEvent();
117             } else {
118                 eventString = EventGenerator.jsonEvent();
119             }
120             producer.send(new ProducerRecord<String, String>(topic, "Event" + i + "Of" + eventCount, eventString));
121             producer.flush();
122             eventsSentCount++;
123             System.out.println("****** Sent event No. " + eventsSentCount + " ******");
124         }
125         System.out.println(KafkaEventProducer.class.getCanonicalName() + ": completed");
126     }
127
128     /**
129      * Gets the events sent count.
130      *
131      * @return the events sent count
132      */
133     public long getEventsSentCount() {
134         return eventsSentCount;
135     }
136
137     /**
138      * Shutdown.
139      */
140     public void shutdown() {
141         System.out.println(KafkaEventProducer.class.getCanonicalName() + ": stopping");
142
143         stopFlag = true;
144
145         while (producerThread.isAlive()) {
146             ThreadUtilities.sleep(10);
147         }
148
149         System.out.println(KafkaEventProducer.class.getCanonicalName() + ": stopped");
150     }
151 }