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