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