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.mr.dme.client;
26 import java.io.IOException;
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;
34 * An example of how to use the Java publisher.
38 public class SimpleExamplePublisher {
39 static String content = null;
40 static String messageSize = null;
41 static String transport = null;
42 static String messageCount = null;
44 public void publishMessage(String producerFilePath) throws IOException, InterruptedException {
46 // create our publisher
48 // publish some messages
51 StringBuilder sb = new StringBuilder();
52 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(producerFilePath);
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++) {
60 pub.send(sb.toString());
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++) {
68 pub.send("Key", sb.toString());
70 } else if (content.equalsIgnoreCase("application/json")) {
71 for (int i = 0; i < Integer.parseInt(messageCount); i++) {
73 final JSONObject msg12 = new JSONObject();
74 msg12.put("Name", "DMaaP Reference Client to Test jason Message");
76 pub.send(msg12.toString());
83 // close the publisher to make sure everything's sent before exiting.
85 // publisher interface allows the app to get the set of unsent messages.
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");
92 System.out.println("Clean exit; all messages sent.");
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));
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));
111 public static void main(String[] args) throws InterruptedException, Exception {
113 String producerFilePath = args[0];
115 messageSize = args[2];
117 messageCount = args[4];
123 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
125 publisher.publishMessage("D:\\SG\\producer.properties");