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