d8b19790a1230b412487915364a084f99bdd0717
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / test / clients / ConsolePublisher.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22
23 package org.onap.dmaap.mr.test.clients;
24
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;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
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;
38
39 /**
40  * A simple publisher that reads from std in, sending each line as a message. 
41  * @author author
42  */
43 public class ConsolePublisher
44 {
45
46         private static final Logger logger = LoggerFactory.getLogger(ConsolePublisher.class);
47     private ConsolePublisher() {
48     }
49         public static void main ( String[] args ) throws IOException //throws IOException, InterruptedException
50         {
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";
53
54                 // read the topic name from the command line
55                 final String topic = args.length > 1 ? args[1] : "TEST-TOPIC";
56
57                 // read the topic name from the command line
58                 final String partition = args.length > 2 ? args[2] : UUID.randomUUID ().toString ();
59
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;
64
65                 // create our publisher
66                 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher ( hosts, topic, maxBatchSize, maxAgeMs, withGzip );
67
68                 final BufferedReader cin = new BufferedReader ( new InputStreamReader ( System.in ) );
69                 try
70                 {
71                         String line = null;
72                         while ( ( line = cin.readLine () ) != null )
73                         {
74                                 pub.send ( partition, line );
75                         }
76                 }
77                 finally
78                 {
79                         List<message> leftovers = null;
80                         try
81                         {
82                                 leftovers = pub.close ( 10, TimeUnit.SECONDS );
83                         }
84                         catch ( InterruptedException e )
85                         {
86                             logger.error( "Send on close interrupted." );
87                             Thread.currentThread().interrupt();
88                         }
89                         for ( message m : leftovers )
90                         {
91                             logger.error( "Unsent message: " + m.fMsg );
92                         }
93                 }
94         }
95 }