1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
23 package org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.dme.client;
26 import java.io.IOException;
27 import java.util.List;
30 import java.util.concurrent.TimeUnit;
32 import javax.ws.rs.core.MultivaluedMap;
34 import org.json.JSONObject;
35 import org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.client.MRBatchingPublisher;
36 import org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.client.MRClientFactory;
37 import org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.client.MRPublisher.message;
40 * An example of how to use the Java publisher.
44 public class SimpleExamplePublisher {
45 static String content = null;
46 static String messageSize = null;
47 static String transport = null;
48 static String messageCount = null;
50 public void publishMessage(String producerFilePath) throws IOException, InterruptedException, Exception {
52 // create our publisher
54 // publish some messages
57 StringBuilder sb = new StringBuilder();
58 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(producerFilePath);
60 if (content.equalsIgnoreCase("text/plain")) {
61 for (int i = 0; i < Integer.parseInt(messageCount); i++) {
62 for (int j = 0; j < Integer.parseInt(messageSize); j++) {
66 pub.send(sb.toString());
68 } else if (content.equalsIgnoreCase("application/cambria")) {
69 for (int i = 0; i < Integer.parseInt(messageCount); i++) {
70 for (int j = 0; j < Integer.parseInt(messageSize); j++) {
74 pub.send("Key", sb.toString());
76 } else if (content.equalsIgnoreCase("application/json")) {
77 for (int i = 0; i < Integer.parseInt(messageCount); i++) {
79 final JSONObject msg12 = new JSONObject();
80 msg12.put("Name", "DMaaP Reference Client to Test jason Message");
82 pub.send(msg12.toString());
89 // close the publisher to make sure everything's sent before exiting.
91 // publisher interface allows the app to get the set of unsent messages.
93 // write them to disk, for example, to try to send them later.
94 /* final List<message> stuck = pub.close(20, TimeUnit.SECONDS);
95 if (stuck.size() > 0) {
96 System.err.println(stuck.size() + " messages unsent");
98 System.out.println("Clean exit; all messages sent.");
101 if (transport.equalsIgnoreCase("HTTP")) {
102 MultivaluedMap<String, Object> headersMap = MRClientFactory.HTTPHeadersMap;
103 for (String key : headersMap.keySet()) {
104 System.out.println("Header Key " + key);
105 System.out.println("Header Value " + headersMap.get(key));
108 Map<String, String> dme2headersMap = MRClientFactory.DME2HeadersMap;
109 for (String key : dme2headersMap.keySet()) {
110 System.out.println("Header Key " + key);
111 System.out.println("Header Value " + dme2headersMap.get(key));
117 public static void main(String[] args) throws InterruptedException, Exception {
119 String producerFilePath = args[0];
121 messageSize = args[2];
123 messageCount = args[4];
124 /*String producerFilePath = null;
128 messageCount = null;*/
129 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
131 publisher.publishMessage("D:\\SG\\producer.properties");