b5d83bb15ca80f47175bc52ccad6a9a57829fbc6
[dmaap/messagerouter/dmaapclient.git] / src / main / 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  *  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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22
23 package org.onap.dmaap.mr.dme.client;
24
25
26 import java.io.IOException;
27 import java.util.Map;
28 import javax.ws.rs.core.MultivaluedMap;
29 import org.json.JSONObject;
30 import org.onap.dmaap.mr.client.MRBatchingPublisher;
31 import org.onap.dmaap.mr.client.MRClientFactory;
32
33 /**
34  * An example of how to use the Java publisher.
35  * 
36  * @author author
37  */
38 public class SimpleExamplePublisher {
39         static String content = null;
40         static String messageSize = null;
41         static String transport = null;
42         static String messageCount = null;
43
44         public void publishMessage(String producerFilePath) throws IOException, InterruptedException {
45
46                 // create our publisher
47                 
48                 // publish some messages
49                 
50                 
51                 StringBuilder sb = new StringBuilder();
52                 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(producerFilePath);
53                 
54                 if (content.equalsIgnoreCase("text/plain")) {
55                         for (int i = 0; i < Integer.parseInt(messageCount); i++) {
56                                 for (int j = 0; j < Integer.parseInt(messageSize); j++) {
57                                         sb.append("T");
58                                 }
59
60                                 pub.send(sb.toString());
61                         }
62                 } else if (content.equalsIgnoreCase("application/cambria")) {
63                         for (int i = 0; i < Integer.parseInt(messageCount); i++) {
64                                 for (int j = 0; j < Integer.parseInt(messageSize); j++) {
65                                         sb.append("C");
66                                 }
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                 // ...
82
83                 // close the publisher to make sure everything's sent before exiting.
84                 // The batching
85                 // publisher interface allows the app to get the set of unsent messages.
86                 // It could
87                 // write them to disk, for example, to try to send them later.
88         /*      final List<message> stuck = pub.close(20, TimeUnit.SECONDS);
89                 if (stuck.size() > 0) {
90                         System.err.println(stuck.size() + " messages unsent");
91                 } else {
92                         System.out.println("Clean exit; all messages sent.");
93                 }*/
94
95                 if (transport.equalsIgnoreCase("HTTP")) {
96                         MultivaluedMap<String, Object> headersMap = MRClientFactory.getHTTPHeadersMap();
97                         for (String key : headersMap.keySet()) {
98                                 System.out.println("Header Key " + key);
99                                 System.out.println("Header Value " + headersMap.get(key));
100                         }
101                 } else {
102                         Map<String, String> dme2headersMap = MRClientFactory.DME2HeadersMap;
103                         for (String key : dme2headersMap.keySet()) {
104                                 System.out.println("Header Key " + key);
105                                 System.out.println("Header Value " + dme2headersMap.get(key));
106                         }
107                 }
108
109         }
110
111         public static void main(String[] args) throws InterruptedException, Exception {
112
113                 String producerFilePath = args[0];
114                 content = args[1];
115                 messageSize = args[2];
116                 transport = args[3];
117                 messageCount = args[4];
118                 
119                 
120                 
121                 
122                 
123                 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
124
125                 publisher.publishMessage("D:\\SG\\producer.properties");
126         }
127
128 }