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.test.clients;
27 import org.onap.dmaap.mr.client.MRBatchingPublisher;
28 import org.onap.dmaap.mr.client.MRClientFactory;
29 import org.onap.dmaap.mr.client.MRPublisher.Message;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.util.List;
37 import java.util.UUID;
38 import java.util.concurrent.TimeUnit;
41 * A simple publisher that reads from std in, sending each line as a message.
45 public class ConsolePublisher {
47 private static final Logger logger = LoggerFactory.getLogger(ConsolePublisher.class);
49 private ConsolePublisher() {
52 public static void main(String[] args) throws IOException //throws IOException, InterruptedException
54 // read the hosts(s) from the command line
55 final String hosts = args.length > 0 ? args[0] : "mr1.onap.com,mr2.onap.com,mr3.onap.com";
57 // read the topic name from the command line
58 final String topic = args.length > 1 ? args[1] : "TEST-TOPIC";
60 // read the topic name from the command line
61 final String partition = args.length > 2 ? args[2] : UUID.randomUUID().toString();
63 // set up some batch limits and the compression flag
64 final int maxBatchSize = 100;
65 final long maxAgeMs = 250;
66 final boolean withGzip = false;
68 // create our publisher
69 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(hosts, topic, maxBatchSize, maxAgeMs, withGzip);
71 final BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
74 while ((line = cin.readLine()) != null) {
75 pub.send(partition, line);
78 List<Message> leftovers = null;
80 leftovers = pub.close(10, TimeUnit.SECONDS);
81 } catch (InterruptedException e) {
82 logger.error("Send on close interrupted.");
83 Thread.currentThread().interrupt();
85 for (Message m : leftovers) {
86 logger.error("Unsent message: " + m.fMsg);