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