2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap;
27 import java.util.List;
28 import java.util.Properties;
29 import java.util.UUID;
30 import java.util.concurrent.TimeUnit;
32 import com.att.nsa.mr.client.MRConsumer;
33 import org.json.JSONObject;
34 import org.onap.appc.adapter.messaging.dmaap.impl.DmaapUtil;
36 import com.att.nsa.mr.client.MRBatchingPublisher;
37 import com.att.nsa.mr.client.MRClientFactory;
38 import com.att.nsa.mr.client.MRPublisher.message;
42 *An example of how to use the Java publisher.
44 public class SimpleExamplePublisher
47 public static void main(String []args) throws InterruptedException, Exception{
49 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
53 String topicProducerPropFileName = DmaapUtil.createProducerPropFile("org.onap.appc.UNIT-TEST", null);
56 publisher.publishMessage(topicProducerPropFileName,i);
64 public void publishMessage( String producerFilePath,int count ) throws IOException, InterruptedException, Exception
66 // create our publisher
67 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher (producerFilePath);
68 // publish some messages
69 final JSONObject msg1 = new JSONObject ();
70 msg1.put ( "Partition:2", "Message:" +count);
71 //msg1.put ( "greeting", "Hello .." );
73 pub.send ( "2", msg1.toString());
74 // close the publisher to make sure everything's sent before exiting. The batching
75 // publisher interface allows the app to get the set of unsent messages. It could
76 // write them to disk, for example, to try to send them later.
77 final List<message> stuck = pub.close ( 20, TimeUnit.SECONDS );
78 if ( stuck.size () > 0 )
80 System.err.println ( stuck.size() + " messages unsent" );
84 System.out.println ( "Clean exit; all messages sent." );
89 public static void fetchMessage()
95 String topic = "org.onap.appc.UNIT-TEST";
96 Properties props = new Properties();
98 props.put("group", "group1");
99 String topicConsumerPropFileName1 = DmaapUtil.createConsumerPropFile(topic,props);
100 final MRConsumer consumer1 = MRClientFactory.createConsumer ( topicConsumerPropFileName1);
102 props = new Properties();
103 props.put("id", "2");
104 props.put("group", "group2");
105 String topicConsumerPropFileName2 = DmaapUtil.createConsumerPropFile(topic,props);
106 final MRConsumer consumer2 = MRClientFactory.createConsumer ( topicConsumerPropFileName2);
108 for ( String msg : consumer1.fetch () )
111 System.out.println ( "consumer1 "+count + ": " + msg );
113 for ( String msg : consumer2.fetch () )
116 System.out.println ( "consumer1 "+count + ": " + msg );
121 catch ( Exception x )
123 System.out.println("inside cons exc");
124 System.err.println ( x.getClass().getName () + ": " + x.getMessage () );