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.MRClientFactory;
34 import org.onap.dmaap.mr.client.MRClientBuilders.PublisherBuilder;
35 import org.onap.dmaap.mr.client.MRPublisher.message;
37 public class SamplePublisher {
38 public static void main ( String[] args ) throws IOException, InterruptedException
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" );
44 // read the topic name from the command line
46 final String topic = ( args.length > 1 ? args[1] : "com.att.app.dmaap.mr.testingTopic" );
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;
53 // create our publisher
55 final MRBatchingPublisher pub = new PublisherBuilder ().
57 onTopic ( topic ).limitBatch(maxBatchSize, maxAgeMs).
58 authenticatedBy ( "CG0TXc2Aa3v8LfBk", "pj2rhxJWKP23pgy8ahMnjH88" ).
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 () );
67 final JSONObject msg2 = new JSONObject ();
68 msg2.put ( "now", System.currentTimeMillis () );
69 pub.send ( "MyOtherPartitionKey", msg2.toString () );
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 );
79 LOG.warn ( stuck.size() + " messages unsent" );
83 LOG.info ( "Clean exit; all messages sent." );