[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / test / clients / SampleConsumer.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 package org.onap.dmaap.mr.test.clients;
25
26 import org.onap.dmaap.mr.client.MRClientFactory;
27 import org.onap.dmaap.mr.client.MRConsumer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.LinkedList;
32
33 public class SampleConsumer {
34     private SampleConsumer() {
35     }
36
37     public static void main(String[] args) {
38         final Logger logger = LoggerFactory.getLogger(SampleConsumer.class);
39
40
41         logger.info("Sample Consumer Class executing");
42         final String topic = "org.onap.dmaap.mr.testingTopic";
43         final String url = (args.length > 1 ? args[1] : "localhost:8181");
44         final String group = (args.length > 2 ? args[2] : "grp");
45
46         final String id = (args.length > 3 ? args[3] : "1");
47
48         long count = 0;
49         long nextReport = 5000;
50
51         final long startMs = System.currentTimeMillis();
52
53         final LinkedList<String> urlList = new LinkedList<>();
54         for (String u : url.split(",")) {
55             urlList.add(u);
56         }
57
58         final MRConsumer cc = MRClientFactory.createConsumer(urlList, topic, group, id, 10 * 1000, 1000, null, "CG0TXc2Aa3v8LfBk", "pj2rhxJWKP23pgy8ahMnjH88");
59         try {
60             while (true) {
61                 for (String msg : cc.fetch()) {
62                     logger.info("" + (++count) + ": " + msg);
63                 }
64
65                 if (count > nextReport) {
66                     nextReport += 5000;
67
68                     final long endMs = System.currentTimeMillis();
69                     final long elapsedMs = endMs - startMs;
70                     final double elapsedSec = elapsedMs / 1000.0;
71                     final double eps = count / elapsedSec;
72                     logger.info("Consumed " + count + " in " + elapsedSec + "; " + eps + " eps");
73                 }
74                 logger.info("" + (++count) + ": consumed message");
75             }
76         } catch (Exception x) {
77             logger.error(x.getClass().getName() + ": " + x.getMessage());
78             throw new IllegalArgumentException(x);
79         }
80     }
81 }