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