First part of onap rename
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / openecomp / appc / adapter / messaging / dmaap / SimpleExamplePublisher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.messaging.dmaap;
26
27 import java.io.*;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.UUID;
31 import java.util.concurrent.TimeUnit;
32
33 import com.att.nsa.mr.client.MRConsumer;
34 import org.json.JSONObject;
35 import org.onap.appc.adapter.messaging.dmaap.impl.DmaapUtil;
36
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;
40
41
42 /**
43  *An example of how to use the Java publisher.
44  */
45 public class SimpleExamplePublisher
46 {
47
48     public static void main(String []args) throws InterruptedException, Exception{
49         int msgCount = 1;
50         SimpleExamplePublisher publisher = new SimpleExamplePublisher();
51
52         int i=0;
53
54         String topicProducerPropFileName = DmaapUtil.createProducerPropFile("org.onap.appc.UNIT-TEST", null);
55         while (i< msgCount)
56         {
57             publisher.publishMessage(topicProducerPropFileName,i);
58             i++;
59         }
60
61         fetchMessage();
62     }
63
64
65     public void publishMessage( String producerFilePath,int count  ) throws IOException, InterruptedException, Exception
66     {
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  .." );
73
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 )
80         {
81             System.err.println ( stuck.size() + " messages unsent" );
82         }
83         else
84         {
85             System.out.println ( "Clean exit; all messages sent." );
86         }
87     }
88
89
90     public static void fetchMessage()
91     {
92         int count = 0;
93
94         try
95         {
96             String topic = "org.onap.appc.UNIT-TEST";
97             Properties props = new Properties();
98             props.put("id", "1");
99             props.put("group", "group1");
100             String topicConsumerPropFileName1 = DmaapUtil.createConsumerPropFile(topic,props);
101             final MRConsumer consumer1 = MRClientFactory.createConsumer ( topicConsumerPropFileName1);
102
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);
108
109             for ( String msg : consumer1.fetch () )
110             {
111                 count++;
112                 System.out.println ( "consumer1 "+count + ": " + msg );
113             }
114             for ( String msg : consumer2.fetch () )
115             {
116                 count++;
117                 System.out.println ( "consumer1 "+count + ": " + msg );
118             }
119
120
121         }
122         catch ( Exception x )
123         {
124             System.out.println("inside cons exc");
125             System.err.println ( x.getClass().getName () + ": " + x.getMessage () );
126         }
127     }
128 }
129
130
131
132
133
134
135
136
137