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 *******************************************************************************/
22 package org.onap.dmaap.mr.test.clients;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
28 import org.json.JSONObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
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;
36 public class SamplePublisher {
37 public static void main ( String[] args ) throws IOException, InterruptedException
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" );
43 // read the topic name from the command line
45 final String topic = ( args.length > 1 ? args[1] : "com.att.app.dmaap.mr.testingTopic" );
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;
52 // create our publisher
54 final MRBatchingPublisher pub = new PublisherBuilder ().
56 onTopic ( topic ).limitBatch(maxBatchSize, maxAgeMs).
57 authenticatedBy ( "CG0TXc2Aa3v8LfBk", "pj2rhxJWKP23pgy8ahMnjH88" ).
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 () );
66 final JSONObject msg2 = new JSONObject ();
67 msg2.put ( "now", System.currentTimeMillis () );
68 pub.send ( "MyOtherPartitionKey", msg2.toString () );
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 );
78 LOG.warn ( stuck.size() + " messages unsent" );
82 LOG.info ( "Clean exit; all messages sent." );