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.testsuites.integration.uservice.adapt.kafka;
23 import com.salesforce.kafka.test.junit4.SharedKafkaTestResource;
25 import java.util.concurrent.TimeUnit;
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;
34 * The Class KafkaEventProducer.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
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;
46 private final Thread producerThread;
47 private boolean sendEventsFlag = false;
48 private boolean stopFlag = false;
51 * Instantiates a new kafka event producer.
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
59 public KafkaEventProducer(final String topic, final SharedKafkaTestResource sharedKafkaTestResource,
60 final int eventCount, final boolean xmlEvents, final long eventInterval) {
62 this.sharedKafkaTestResource = sharedKafkaTestResource;
63 this.eventCount = eventCount;
64 this.xmlEvents = xmlEvents;
65 this.eventInterval = eventInterval;
67 producerThread = new Thread(this);
68 producerThread.start();
74 * @see java.lang.Runnable#run()
78 final Producer<String, String> producer = sharedKafkaTestResource.getKafkaTestUtils()
79 .getKafkaProducer(StringSerializer.class, StringSerializer.class);
81 while (producerThread.isAlive() && !stopFlag) {
82 ThreadUtilities.sleep(50);
85 sendEventsToTopic(producer);
86 sendEventsFlag = false;
90 producer.close(1000, TimeUnit.MILLISECONDS);
96 public void sendEvents() {
97 sendEventsFlag = true;
101 * Send events to topic.
103 * @param producer the producer
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);
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);
114 String eventString = null;
116 eventString = EventGenerator.xmlEvent();
118 eventString = EventGenerator.jsonEvent();
120 producer.send(new ProducerRecord<String, String>(topic, "Event" + i + "Of" + eventCount, eventString));
123 System.out.println("****** Sent event No. " + eventsSentCount + " ******");
125 System.out.println(KafkaEventProducer.class.getCanonicalName() + ": completed");
129 * Gets the events sent count.
131 * @return the events sent count
133 public long getEventsSentCount() {
134 return eventsSentCount;
140 public void shutdown() {
141 System.out.println(KafkaEventProducer.class.getCanonicalName() + ": stopping");
145 while (producerThread.isAlive()) {
146 ThreadUtilities.sleep(10);
149 System.out.println(KafkaEventProducer.class.getCanonicalName() + ": stopped");