d2b77eff6894ebf770aeacbba5ca943b1fa95be5
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / test / clients / SimpleExamplePublisherWithResponse.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.test.clients;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.util.Properties;
28 import org.json.JSONObject;
29 import org.onap.dmaap.mr.client.MRBatchingPublisher;
30 import org.onap.dmaap.mr.client.MRClientFactory;
31 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
32         /**
33          *An example of how to use the Java publisher. 
34          * @author author
35          *
36          */
37         public class SimpleExamplePublisherWithResponse
38         {
39                 static FileWriter routeWriter= null;
40                 static Properties props=null;   
41                 static FileReader routeReader=null;
42                 
43                 public static void main(String []args) throws InterruptedException, Exception{
44                         
45                         String routeFilePath="src/main/resources/dme2/preferredRoute.txt";
46                         String msgCount = args[0];
47                         SimpleExamplePublisherWithResponse publisher = new SimpleExamplePublisherWithResponse();
48                         File fo= new File(routeFilePath);
49                         if(!fo.exists()){
50                                         routeWriter=new FileWriter(new File (routeFilePath));
51                         }       
52                         routeReader= new FileReader(new File (routeFilePath));
53                         props= new Properties();
54                         int i=0;
55                         while (i< Integer.valueOf(msgCount))
56                         {
57                                 publisher.publishMessage("src/main/resources/dme2/producer.properties",Integer.valueOf(msgCount));
58                                 i++;
59                         }
60                 }
61                 
62                 public void publishMessage ( String producerFilePath , int count ) throws IOException, InterruptedException
63                 {
64                         // create our publisher
65                         final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher (producerFilePath,true);        
66                         // publish some messages
67                         final JSONObject msg1 = new JSONObject ();
68
69                         msg1.put ( "Partition:1", "Message:"+count);
70                         msg1.put ( "greeting", "Hello  .." );
71                         
72                         
73                         pub.send ( "1", msg1.toString());
74                         pub.send ( "1", msg1.toString());
75                         
76                         MRPublisherResponse res= pub.sendBatchWithResponse();
77                         
78                         System.out.println("Pub response->"+res.toString());
79                 }
80                 
81                 
82         }