[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / test / clients / SamplePublisher.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.json.JSONObject;
28 import org.onap.dmaap.mr.client.MRBatchingPublisher;
29 import org.onap.dmaap.mr.client.MRClientBuilders.PublisherBuilder;
30 import org.onap.dmaap.mr.client.MRPublisher.Message;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.io.IOException;
35 import java.util.List;
36 import java.util.concurrent.TimeUnit;
37
38 public class SamplePublisher {
39     public static void main(String[] args) throws IOException, InterruptedException {
40         final Logger logger = LoggerFactory.getLogger(SampleConsumer.class);
41         // read the hosts(s) from the command line
42         final String hosts = (args.length > 0 ? args[0] : "localhost:8181");
43
44         // read the topic name from the command line
45
46         final String topic = (args.length > 1 ? args[1] : "org.onap.dmaap.mr.testingTopic");
47
48         // set up some batch limits and the compression flag
49         final int maxBatchSize = 100;
50         final int maxAgeMs = 250;
51         final boolean withGzip = false;
52
53         // create our publisher
54
55         final MRBatchingPublisher pub = new PublisherBuilder().
56                 usingHosts(hosts).
57                 onTopic(topic).limitBatch(maxBatchSize, maxAgeMs).
58                 authenticatedBy("CG0TXc2Aa3v8LfBk", "pj2rhxJWKP23pgy8ahMnjH88").
59                 build();
60         // publish some messages
61         final JSONObject msg1 = new JSONObject();
62         msg1.put("name", "tttttttttttttttt");
63         msg1.put("greeting", "ooooooooooooooooo");
64         pub.send("MyPartitionKey", msg1.toString());
65
66         final JSONObject msg2 = new JSONObject();
67         msg2.put("now", System.currentTimeMillis());
68         pub.send("MyOtherPartitionKey", msg2.toString());
69
70         // ...
71
72         // close the publisher to make sure everything's sent before exiting. The batching
73         // publisher interface allows the app to get the set of unsent messages. It could
74         // write them to disk, for example, to try to send them later.
75         final List<Message> stuck = pub.close(20, TimeUnit.SECONDS);
76         if (!stuck.isEmpty()) {
77             logger.warn(stuck.size() + " messages unsent");
78         } else {
79             logger.info("Clean exit; all messages sent.");
80         }
81     }
82 }