[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / test / clients / ConsolePublisher.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Modifications Copyright © 2021 Orange.
8  *  ================================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *  ============LICENSE_END=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.test.clients;
26
27 import org.onap.dmaap.mr.client.MRBatchingPublisher;
28 import org.onap.dmaap.mr.client.MRClientFactory;
29 import org.onap.dmaap.mr.client.MRPublisher.Message;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.util.List;
37 import java.util.UUID;
38 import java.util.concurrent.TimeUnit;
39
40 /**
41  * A simple publisher that reads from std in, sending each line as a message.
42  *
43  * @author author
44  */
45 public class ConsolePublisher {
46
47     private static final Logger logger = LoggerFactory.getLogger(ConsolePublisher.class);
48
49     private ConsolePublisher() {
50     }
51
52     public static void main(String[] args) throws IOException //throws IOException, InterruptedException
53     {
54         // read the hosts(s) from the command line
55         final String hosts = args.length > 0 ? args[0] : "mr1.onap.com,mr2.onap.com,mr3.onap.com";
56
57         // read the topic name from the command line
58         final String topic = args.length > 1 ? args[1] : "TEST-TOPIC";
59
60         // read the topic name from the command line
61         final String partition = args.length > 2 ? args[2] : UUID.randomUUID().toString();
62
63         // set up some batch limits and the compression flag
64         final int maxBatchSize = 100;
65         final long maxAgeMs = 250;
66         final boolean withGzip = false;
67
68         // create our publisher
69         final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(hosts, topic, maxBatchSize, maxAgeMs, withGzip);
70
71         final BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
72         try {
73             String line = null;
74             while ((line = cin.readLine()) != null) {
75                 pub.send(partition, line);
76             }
77         } finally {
78             List<Message> leftovers = null;
79             try {
80                 leftovers = pub.close(10, TimeUnit.SECONDS);
81             } catch (InterruptedException e) {
82                 logger.error("Send on close interrupted.");
83                 Thread.currentThread().interrupt();
84             }
85             for (Message m : leftovers) {
86                 logger.error("Unsent message: " + m.fMsg);
87             }
88         }
89     }
90 }