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;
26 import java.time.Duration;
28 import org.apache.kafka.clients.producer.Producer;
29 import org.apache.kafka.clients.producer.ProducerRecord;
30 import org.apache.kafka.common.serialization.StringSerializer;
31 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
32 import org.onap.policy.apex.testsuites.integration.uservice.adapt.events.EventGenerator;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The Class KafkaEventProducer.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class KafkaEventProducer implements Runnable {
42 // Get a reference to the logger
43 private static final Logger LOGGER = LoggerFactory.getLogger(KafkaEventProducer.class);
45 private final String topic;
46 private final SharedKafkaTestResource sharedKafkaTestResource;
47 private final int eventCount;
48 private final boolean xmlEvents;
49 private final long eventInterval;
50 private long eventsSentCount = 0;
52 private final Thread producerThread;
53 private boolean sendEventsFlag = false;
54 private volatile boolean stopFlag = false;
57 * Instantiates a new kafka event producer.
59 * @param topic the topic
60 * @param sharedKafkaTestResource the kafka server address
61 * @param eventCount the event count
62 * @param xmlEvents the xml events
63 * @param eventInterval the event interval
65 public KafkaEventProducer(final String topic, final SharedKafkaTestResource sharedKafkaTestResource,
66 final int eventCount, final boolean xmlEvents, final long eventInterval) {
68 this.sharedKafkaTestResource = sharedKafkaTestResource;
69 this.eventCount = eventCount;
70 this.xmlEvents = xmlEvents;
71 this.eventInterval = eventInterval;
73 producerThread = new Thread(this);
74 producerThread.start();
82 final Producer<String, String> producer = sharedKafkaTestResource.getKafkaTestUtils()
83 .getKafkaProducer(StringSerializer.class, StringSerializer.class);
85 while (producerThread.isAlive() && !stopFlag) {
86 ThreadUtilities.sleep(50);
89 sendEventsToTopic(producer);
90 sendEventsFlag = false;
94 producer.close(Duration.ofMillis(1000));
100 public void sendEvents() {
101 sendEventsFlag = true;
105 * Send events to topic.
107 * @param producer the producer
109 private void sendEventsToTopic(final Producer<String, String> producer) {
110 LOGGER.debug("{} : sending events to Kafka server, event count {}, xmlEvents {}",
111 KafkaEventProducer.class.getName(), eventCount, xmlEvents);
113 for (int i = 0; i < eventCount; i++) {
114 LOGGER.debug("{} : waiting {} milliseconds before sending next event", KafkaEventProducer.class.getName(),
116 ThreadUtilities.sleep(eventInterval);
118 String eventString = null;
120 eventString = EventGenerator.xmlEvent();
122 eventString = EventGenerator.jsonEvent();
124 producer.send(new ProducerRecord<String, String>(topic, "Event" + i + "Of" + eventCount, eventString));
127 LOGGER.debug("****** Sent event No. {} ******\n{}", eventsSentCount, eventString);
129 LOGGER.debug("{}: completed", KafkaEventProducer.class.getName());
133 * Gets the events sent count.
135 * @return the events sent count
137 public long getEventsSentCount() {
138 return eventsSentCount;
144 public void shutdown() {
145 LOGGER.debug("{} : stopping", KafkaEventProducer.class.getName());
149 while (producerThread.isAlive()) {
150 ThreadUtilities.sleep(10);
153 LOGGER.debug("{} : stopped", KafkaEventProducer.class.getName());
156 public boolean isAlive() {
157 return producerThread.isAlive();