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;
27 import java.util.List;
30 import java.util.concurrent.TimeUnit;
32 import javax.ws.rs.core.MultivaluedMap;
34 import org.json.JSONObject;
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;
41 * An example of how to use the Java publisher.
45 public class SimpleExamplePublisher {
46 static String content = null;
47 static String messageSize = null;
48 static String transport = null;
49 static String messageCount = null;
51 public void publishMessage(String producerFilePath) throws IOException, InterruptedException {
53 // create our publisher
55 // publish some messages
58 StringBuilder sb = new StringBuilder();
59 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(producerFilePath);
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++) {
67 pub.send(sb.toString());
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++) {
75 pub.send("Key", sb.toString());
77 } else if (content.equalsIgnoreCase("application/json")) {
78 for (int i = 0; i < Integer.parseInt(messageCount); i++) {
80 final JSONObject msg12 = new JSONObject();
81 msg12.put("Name", "DMaaP Reference Client to Test jason Message");
83 pub.send(msg12.toString());
90 // close the publisher to make sure everything's sent before exiting.
92 // publisher interface allows the app to get the set of unsent messages.
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");
99 System.out.println("Clean exit; all messages sent.");
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));
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));
118 public static void main(String[] args) throws InterruptedException, Exception {
120 String producerFilePath = args[0];
122 messageSize = args[2];
124 messageCount = args[4];
130 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
132 publisher.publishMessage("D:\\SG\\producer.properties");