2 * ========================LICENSE_START=================================
5 * Copyright (C) 2024: OpenInfra Foundation Europe
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 package com.demo.producer.producer;
22 import java.util.UUID;
23 import java.util.Properties;
24 import java.util.concurrent.atomic.AtomicBoolean;
26 import org.apache.kafka.clients.producer.KafkaProducer;
27 import org.apache.kafka.clients.producer.ProducerRecord;
28 import org.json.simple.JSONObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.stereotype.Component;
34 import com.demo.producer.messages.AbstractSimpleKafka;
35 import com.demo.producer.messages.KafkaMessageHandler;
36 import com.demo.producer.messages.MessageHelper;
37 import com.demo.producer.messages.PropertiesHelper;
45 public class SimpleProducer extends AbstractSimpleKafka {
46 private static final Logger log = LoggerFactory.getLogger(SimpleProducer.class);
48 @Value("${vars.time:1000}")
51 private KafkaProducer<String, String> kafkaProducer;
52 private final AtomicBoolean closed = new AtomicBoolean(false);
54 public void run(String topicName, int numberOfMessages, KafkaMessageHandler callback) throws Exception {
55 for (int i = 0; i < numberOfMessages; i++) {
56 String key = UUID.randomUUID().toString();
57 String message = MessageHelper.getRandomString();
58 if (this.getTopicName() == null) {
59 this.setTopicName(topicName);
61 this.send(topicName, key, message);
67 public void runAlways(String topicName, KafkaMessageHandler callback) throws Exception {
69 String key = UUID.randomUUID().toString();
70 String message = MessageHelper.getRandomString();
71 this.send(topicName, key, message);
76 private String topicName = null;
78 private void setTopicName(String topicName) {
79 this.topicName = topicName;
82 private String getTopicName() {
83 return this.topicName;
86 protected void send(String topicName, String key, String message) throws Exception {
87 String source = SimpleProducer.class.getName();
88 ProducerRecord<String, String> producerRecord = new ProducerRecord<>(topicName, key, message);
89 JSONObject obj = MessageHelper.getMessageLogEntryJSON(source, topicName, key, message);
90 log.info(obj.toJSONString());
91 getKafkaProducer().send(producerRecord);
94 private KafkaProducer<String, String> getKafkaProducer() throws Exception {
95 if (this.kafkaProducer == null) {
96 Properties props = PropertiesHelper.getProperties();
97 this.kafkaProducer = new KafkaProducer<>(props);
99 return this.kafkaProducer;
102 public void shutdown(){
105 log.info(MessageHelper.getSimpleJSONObject("Shutting down producer").toJSONString());
106 getKafkaProducer().close();
107 } catch (Exception e) {
108 log.error("Error shutting down the Producer ", e.getMessage());