2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.kafka;
24 import com.salesforce.kafka.test.junit4.SharedKafkaTestResource;
25 import java.time.Duration;
26 import org.apache.kafka.clients.producer.Producer;
27 import org.apache.kafka.clients.producer.ProducerRecord;
28 import org.apache.kafka.common.serialization.StringSerializer;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The Class KafkaEventProducer.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class KafkaEventProducer implements Runnable {
40 // Get a reference to the logger
41 private static final Logger LOGGER = LoggerFactory.getLogger(KafkaEventProducer.class);
43 private final String topic;
44 private final SharedKafkaTestResource sharedKafkaTestResource;
45 private final int eventCount;
46 private final boolean xmlEvents;
47 private final long eventInterval;
48 private long eventsSentCount = 0;
50 private final Thread producerThread;
51 private boolean sendEventsFlag = false;
52 private volatile boolean stopFlag = false;
55 * Instantiates a new kafka event producer.
57 * @param topic the topic
58 * @param sharedKafkaTestResource the kafka server address
59 * @param eventCount the event count
60 * @param xmlEvents the xml events
61 * @param eventInterval the event interval
63 public KafkaEventProducer(final String topic, final SharedKafkaTestResource sharedKafkaTestResource,
64 final int eventCount, final boolean xmlEvents, final long eventInterval) {
66 this.sharedKafkaTestResource = sharedKafkaTestResource;
67 this.eventCount = eventCount;
68 this.xmlEvents = xmlEvents;
69 this.eventInterval = eventInterval;
71 producerThread = new Thread(this);
72 producerThread.start();
80 final Producer<String, String> producer = sharedKafkaTestResource.getKafkaTestUtils()
81 .getKafkaProducer(StringSerializer.class, StringSerializer.class);
83 while (producerThread.isAlive() && !stopFlag) {
84 ThreadUtilities.sleep(50);
87 sendEventsToTopic(producer);
88 sendEventsFlag = false;
92 producer.close(Duration.ofMillis(1000));
98 public void sendEvents() {
99 sendEventsFlag = true;
103 * Send events to topic.
105 * @param producer the producer
107 private void sendEventsToTopic(final Producer<String, String> producer) {
108 LOGGER.debug("{} : sending events to Kafka server, event count {}, xmlEvents {}",
109 KafkaEventProducer.class.getName(), eventCount, xmlEvents);
111 for (int i = 0; i < eventCount; i++) {
112 LOGGER.debug("{} : waiting {} milliseconds before sending next event", KafkaEventProducer.class.getName(),
114 ThreadUtilities.sleep(eventInterval);
116 String eventString = null;
118 eventString = EventGenerator.xmlEvent();
120 eventString = EventGenerator.jsonEvent();
122 producer.send(new ProducerRecord<String, String>(topic, "Event" + i + "Of" + eventCount, eventString));
125 LOGGER.debug("****** Sent event No. {} ******\n{}", eventsSentCount, eventString);
127 LOGGER.debug("{}: completed", KafkaEventProducer.class.getName());
131 * Gets the events sent count.
133 * @return the events sent count
135 public long getEventsSentCount() {
136 return eventsSentCount;
142 public void shutdown() {
143 LOGGER.debug("{} : stopping", KafkaEventProducer.class.getName());
147 while (producerThread.isAlive()) {
148 ThreadUtilities.sleep(10);
151 LOGGER.debug("{} : stopped", KafkaEventProducer.class.getName());
154 public boolean isAlive() {
155 return producerThread.isAlive();