2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.messaging.dmaap;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.UUID;
31 import java.util.concurrent.TimeUnit;
33 import com.att.nsa.mr.client.MRConsumer;
34 import org.json.JSONObject;
35 import org.onap.appc.adapter.messaging.dmaap.impl.DmaapUtil;
37 import com.att.nsa.mr.client.MRBatchingPublisher;
38 import com.att.nsa.mr.client.MRClientFactory;
39 import com.att.nsa.mr.client.MRPublisher.message;
43 *An example of how to use the Java publisher.
45 public class SimpleExamplePublisher
48 public static void main(String []args) throws InterruptedException, Exception{
50 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
54 String topicProducerPropFileName = DmaapUtil.createProducerPropFile("org.onap.appc.UNIT-TEST", null);
57 publisher.publishMessage(topicProducerPropFileName,i);
65 public void publishMessage( String producerFilePath,int count ) throws IOException, InterruptedException, Exception
67 // create our publisher
68 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher (producerFilePath);
69 // publish some messages
70 final JSONObject msg1 = new JSONObject ();
71 msg1.put ( "Partition:2", "Message:" +count);
72 //msg1.put ( "greeting", "Hello .." );
74 pub.send ( "2", msg1.toString());
75 // close the publisher to make sure everything's sent before exiting. The batching
76 // publisher interface allows the app to get the set of unsent messages. It could
77 // write them to disk, for example, to try to send them later.
78 final List<message> stuck = pub.close ( 20, TimeUnit.SECONDS );
79 if ( stuck.size () > 0 )
81 System.err.println ( stuck.size() + " messages unsent" );
85 System.out.println ( "Clean exit; all messages sent." );
90 public static void fetchMessage()
96 String topic = "org.onap.appc.UNIT-TEST";
97 Properties props = new Properties();
99 props.put("group", "group1");
100 String topicConsumerPropFileName1 = DmaapUtil.createConsumerPropFile(topic,props);
101 final MRConsumer consumer1 = MRClientFactory.createConsumer ( topicConsumerPropFileName1);
103 props = new Properties();
104 props.put("id", "2");
105 props.put("group", "group2");
106 String topicConsumerPropFileName2 = DmaapUtil.createConsumerPropFile(topic,props);
107 final MRConsumer consumer2 = MRClientFactory.createConsumer ( topicConsumerPropFileName2);
109 for ( String msg : consumer1.fetch () )
112 System.out.println ( "consumer1 "+count + ": " + msg );
114 for ( String msg : consumer2.fetch () )
117 System.out.println ( "consumer1 "+count + ": " + msg );
122 catch ( Exception x )
124 System.out.println("inside cons exc");
125 System.err.println ( x.getClass().getName () + ": " + x.getMessage () );