ICS sample producer and consumer
[nonrtric.git] / sample-services / ics-producer-consumer / producer / src / main / java / com / demo / producer / producer / SimpleProducer.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  *
5  * Copyright (C) 2024: OpenInfra Foundation Europe
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package com.demo.producer.producer;
21
22 import java.util.UUID;
23 import java.util.Properties;
24 import java.util.concurrent.atomic.AtomicBoolean;
25
26 import org.apache.kafka.clients.producer.KafkaProducer;
27 import org.apache.kafka.clients.producer.ProducerRecord;
28 import org.json.simple.JSONObject;
29
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;
38
39 import lombok.Getter;
40 import lombok.Setter;
41
42 @Component
43 @Getter
44 @Setter
45 public class SimpleProducer extends AbstractSimpleKafka {
46     private static final Logger log = LoggerFactory.getLogger(SimpleProducer.class);
47
48     @Value("${vars.time:1000}")
49     private int TIME;
50
51     private KafkaProducer<String, String> kafkaProducer;
52     private final AtomicBoolean closed = new AtomicBoolean(false);
53
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);
60             }
61             this.send(topicName, key, message);
62             Thread.sleep(TIME);
63         }
64         this.shutdown();
65     }
66
67     public void runAlways(String topicName, KafkaMessageHandler callback) throws Exception {
68         while (true) {
69             String key = UUID.randomUUID().toString();
70             String message = MessageHelper.getRandomString();
71             this.send(topicName, key, message);
72             Thread.sleep(TIME);
73         }
74     }
75
76     private String topicName = null;
77
78     private void setTopicName(String topicName) {
79         this.topicName = topicName;
80     }
81
82     private String getTopicName() {
83         return this.topicName;
84     }
85
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);
92     }
93
94     private KafkaProducer<String, String> getKafkaProducer() throws Exception {
95         if (this.kafkaProducer == null) {
96             Properties props = PropertiesHelper.getProperties();
97             this.kafkaProducer = new KafkaProducer<>(props);
98         }
99         return this.kafkaProducer;
100     }
101
102     public void shutdown(){
103         closed.set(true);
104         try {
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());
109         }
110
111     }
112 }