1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.dme.client;
28 import org.json.JSONObject;
29 import org.onap.dmaap.mr.client.MRBatchingPublisher;
30 import org.onap.dmaap.mr.client.MRClientFactory;
32 import javax.ws.rs.core.MultivaluedMap;
33 import java.io.IOException;
37 * An example of how to use the Java publisher.
41 public class SimpleExamplePublisher {
42 static String content = null;
43 static String messageSize = null;
44 static String transport = null;
45 static String messageCount = null;
47 public void publishMessage(String producerFilePath) throws IOException, InterruptedException {
49 // create our publisher
50 // publish some messages
52 StringBuilder sb = new StringBuilder();
53 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(producerFilePath);
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++) {
61 pub.send(sb.toString());
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++) {
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());
81 // close the publisher to make sure everything's sent before exiting.
83 // publisher interface allows the app to get the set of unsent messages.
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");
90 System.out.println("Clean exit; all messages sent.");
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));
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));
109 public static void main(String[] args) throws InterruptedException, Exception {
111 String producerFilePath = args[0];
113 messageSize = args[2];
115 messageCount = args[4];
118 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
120 publisher.publishMessage("D:\\SG\\producer.properties");