[KAFKA] Add docker-compose to sample project
[dmaap/kafka11aaf.git] / sampleClient / src / main / java / org / onap / dmaap / kafka / sample / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * dmaap-kafka-client
4  * ================================================================================
5  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
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
21 package org.onap.dmaap.kafka.sample;
22
23 import java.util.List;
24 import org.apache.kafka.clients.producer.RecordMetadata;
25 import org.onap.dmaap.kafka.OnapKafkaClient;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.boot.CommandLineRunner;
30 import org.springframework.boot.SpringApplication;
31 import org.springframework.boot.autoconfigure.SpringBootApplication;
32
33 @SpringBootApplication
34 public class Main implements CommandLineRunner{
35
36     private final Logger log = LoggerFactory.getLogger(OnapKafkaClient.class.getName());
37
38     @Autowired
39     private SampleConfiguration configuration;
40
41     public static void main(String[] args) {
42         SpringApplication.run(Main.class, args);
43     }
44
45     @Override
46     public void run(String... args) throws InterruptedException {
47         OnapKafkaClient handler = new OnapKafkaClient(configuration);
48         String testTopic = configuration.getConsumerTopics().get(0);
49         for (int i = 0; i < 5; i++) {
50             RecordMetadata recordMetadata = handler.publishToTopic(testTopic, "dummy-message-"+i);
51             if (recordMetadata != null) {
52                 log.info("Topic: {}, Partition: {}, Offset: {}", recordMetadata.topic(), recordMetadata.partition(), recordMetadata.offset());
53             }
54         }
55         int fetch = 0;
56         while (true) {
57             fetch++;
58             log.info("Fetch {} from topic: {}", fetch, testTopic);
59             List<String> res = handler.fetchFromTopic(testTopic);
60             log.info("Messages from fetch {}: " + res, fetch);
61             Thread.sleep(3000);
62         }
63     }
64 }