50fd22c50cb47a6617dd2f055d35471d43fed6cb
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / test / clients / SimpleExamplePublisher.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
23 package org.onap.dmaap.mr.test.clients;
24
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.util.List;
30 import java.util.Properties;
31 import java.util.concurrent.TimeUnit;
32
33 import org.json.JSONObject;
34
35 import org.onap.dmaap.mr.client.MRBatchingPublisher;
36 import org.onap.dmaap.mr.client.MRClientFactory;
37 import org.onap.dmaap.mr.client.MRPublisher.message;
38
39 /**
40  * An example of how to use the Java publisher. 
41  * @author author
42  */
43 public class SimpleExamplePublisher
44 {
45         static FileWriter routeWriter= null;
46         static Properties props=null;   
47         static FileReader routeReader=null;
48         public void publishMessage ( String producerFilePath  ) throws IOException, InterruptedException, Exception
49         {
50                                 
51                 // create our publisher
52                 final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher (producerFilePath);     
53                 // publish some messages
54                 final JSONObject msg1 = new JSONObject ();
55                 msg1.put ( "Name", "Sprint" );
56                 
57                 pub.send ( "First cambria messge" );
58                 pub.send ( "MyPartitionKey", msg1.toString () );
59
60                 final JSONObject msg2 = new JSONObject ();
61                 
62                 
63         
64                 // ...
65
66                 // close the publisher to make sure everything's sent before exiting. The batching
67                 // publisher interface allows the app to get the set of unsent messages. It could
68                 // write them to disk, for example, to try to send them later.
69                 final List<message> stuck = pub.close ( 20, TimeUnit.SECONDS );
70                 if ( stuck.isEmpty() )
71                 {
72                         System.err.println ( stuck.size() + " messages unsent" );
73                 }
74                 else
75                 {
76                         System.out.println ( "Clean exit; all messages sent." );
77                 }
78         }
79         
80         public static void main(String []args) throws InterruptedException, Exception{
81
82                 String routeFilePath="/src/main/resources/dme2/preferredRoute.txt";
83
84                 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
85
86                 
87                 File fo= new File(routeFilePath);
88                 if(!fo.exists()){
89                                 routeWriter=new FileWriter(new File (routeFilePath));
90                 }       
91                 routeReader= new FileReader(new File (routeFilePath));
92                 props= new Properties();
93                 publisher.publishMessage("/src/main/resources/dme2/producer.properties");
94                 }
95         
96 }
97