one more case added in DmaapClientUtilTest.java
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / test / clients / SamplePublisher.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 package org.onap.dmaap.mr.test.clients;
23
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
27
28 import org.json.JSONObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import org.onap.dmaap.mr.client.MRBatchingPublisher;
33 import org.onap.dmaap.mr.client.MRClientFactory;
34 import org.onap.dmaap.mr.client.MRClientBuilders.PublisherBuilder;
35 import org.onap.dmaap.mr.client.MRPublisher.message;
36
37 public class SamplePublisher {
38         public static void main ( String[] args ) throws IOException, InterruptedException
39         {
40                 final Logger LOG = LoggerFactory.getLogger(SampleConsumer.class);
41                 // read the hosts(s) from the command line
42                 final String hosts = ( args.length > 0 ? args[0] : "localhost:8181" );
43
44                 // read the topic name from the command line
45                 
46                 final String topic = ( args.length > 1 ? args[1] : "com.att.app.dmaap.mr.testingTopic" );
47
48                 // set up some batch limits and the compression flag
49                 final int maxBatchSize = 100;
50                 final int maxAgeMs = 250;
51                 final boolean withGzip = false;
52
53                 // create our publisher
54         
55                 final MRBatchingPublisher pub = new PublisherBuilder ().
56                                 usingHosts ( hosts ).
57                                 onTopic ( topic ).limitBatch(maxBatchSize, maxAgeMs).                           
58                                 authenticatedBy ( "CG0TXc2Aa3v8LfBk", "pj2rhxJWKP23pgy8ahMnjH88" ).
59                                 build ()
60                         ;
61                 // publish some messages
62                 final JSONObject msg1 = new JSONObject ();
63                 msg1.put ( "name", "tttttttttttttttt" );
64                 msg1.put ( "greeting", "ooooooooooooooooo" );
65                 pub.send ( "MyPartitionKey", msg1.toString () );
66
67                 final JSONObject msg2 = new JSONObject ();
68                 msg2.put ( "now", System.currentTimeMillis () );
69                 pub.send ( "MyOtherPartitionKey", msg2.toString () );
70
71                 // ...
72
73                 // close the publisher to make sure everything's sent before exiting. The batching
74                 // publisher interface allows the app to get the set of unsent messages. It could
75                 // write them to disk, for example, to try to send them later.
76                 final List<message> stuck = pub.close ( 20, TimeUnit.SECONDS );
77                 if ( stuck.isEmpty())
78                 {
79                         LOG.warn ( stuck.size() + " messages unsent" );
80                 }
81                 else
82                 {
83                         LOG.info ( "Clean exit; all messages sent." );
84                 }
85         }
86 }