6a6777459d953965e24f987ed4e5b0da684d2738
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / dme / client / SimpleExamplePublisher.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.dme.client;
26
27
28 import org.json.JSONObject;
29 import org.onap.dmaap.mr.client.MRBatchingPublisher;
30 import org.onap.dmaap.mr.client.MRClientFactory;
31
32 import javax.ws.rs.core.MultivaluedMap;
33 import java.io.IOException;
34 import java.util.Map;
35
36 /**
37  * An example of how to use the Java publisher.
38  *
39  * @author author
40  */
41 public class SimpleExamplePublisher {
42     static String content = null;
43     static String messageSize = null;
44     static String transport = null;
45     static String messageCount = null;
46
47     public void publishMessage(String producerFilePath) throws IOException, InterruptedException {
48
49         // create our publisher
50         // publish some messages
51
52         StringBuilder sb = new StringBuilder();
53         final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(producerFilePath);
54
55         if (content.equalsIgnoreCase("text/plain")) {
56             for (int i = 0; i < Integer.parseInt(messageCount); i++) {
57                 for (int j = 0; j < Integer.parseInt(messageSize); j++) {
58                     sb.append("T");
59                 }
60
61                 pub.send(sb.toString());
62             }
63         } else if (content.equalsIgnoreCase("application/cambria")) {
64             for (int i = 0; i < Integer.parseInt(messageCount); i++) {
65                 for (int j = 0; j < Integer.parseInt(messageSize); j++) {
66                     sb.append("C");
67                 }
68                 pub.send("Key", sb.toString());
69             }
70         } else if (content.equalsIgnoreCase("application/json")) {
71             for (int i = 0; i < Integer.parseInt(messageCount); i++) {
72
73                 final JSONObject msg12 = new JSONObject();
74                 msg12.put("Name", "DMaaP Reference Client to Test jason Message");
75
76                 pub.send(msg12.toString());
77
78             }
79         }
80
81         // close the publisher to make sure everything's sent before exiting.
82         // The batching
83         // publisher interface allows the app to get the set of unsent messages.
84         // It could
85         // write them to disk, for example, to try to send them later.
86     /*  final List<message> stuck = pub.close(20, TimeUnit.SECONDS);
87         if (stuck.size() > 0) {
88             System.err.println(stuck.size() + " messages unsent");
89         } else {
90             System.out.println("Clean exit; all messages sent.");
91         }*/
92
93         if (transport.equalsIgnoreCase("HTTP")) {
94             MultivaluedMap<String, Object> headersMap = MRClientFactory.getHTTPHeadersMap();
95             for (String key : headersMap.keySet()) {
96                 System.out.println("Header Key " + key);
97                 System.out.println("Header Value " + headersMap.get(key));
98             }
99         } else {
100             Map<String, String> dme2headersMap = MRClientFactory.DME2HeadersMap;
101             for (String key : dme2headersMap.keySet()) {
102                 System.out.println("Header Key " + key);
103                 System.out.println("Header Value " + dme2headersMap.get(key));
104             }
105         }
106
107     }
108
109     public static void main(String[] args) throws InterruptedException, Exception {
110
111         String producerFilePath = args[0];
112         content = args[1];
113         messageSize = args[2];
114         transport = args[3];
115         messageCount = args[4];
116
117
118         SimpleExamplePublisher publisher = new SimpleExamplePublisher();
119
120         publisher.publishMessage("D:\\SG\\producer.properties");
121     }
122
123 }