1 /*******************************************************************************
 
   2  *  ============LICENSE_START=======================================================
 
   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
 
  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=========================================================
 
  19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  21  *******************************************************************************/
 
  23 package org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.test.clients;
 
  26 import java.io.FileReader;
 
  27 import java.io.FileWriter;
 
  28 import java.io.IOException;
 
  29 import java.io.PrintWriter;
 
  30 import java.util.List;
 
  31 import java.util.Properties;
 
  32 import java.util.concurrent.TimeUnit;
 
  34 import org.json.JSONObject;
 
  35 import org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.client.MRBatchingPublisher;
 
  36 import org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.client.MRClientFactory;
 
  37 import org.onap.dmaap.messagerouter.dmaapclient.nsa.mr.client.MRPublisher.message;
 
  40  * An example of how to use the Java publisher. 
 
  43 public class SimpleExamplePublisher
 
  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
 
  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                 //msg1.put ( "greeting", "Hello  .." );
 
  57                 pub.send ( "First cambria messge" );
 
  58                 pub.send ( "MyPartitionKey", msg1.toString () );
 
  60                 final JSONObject msg2 = new JSONObject ();
 
  61                 //msg2.put ( "mrclient1", System.currentTimeMillis () );
 
  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.size () > 0 )
 
  72                         System.err.println ( stuck.size() + " messages unsent" );
 
  76                         System.out.println ( "Clean exit; all messages sent." );
 
  80         public static void main(String []args) throws InterruptedException, Exception{
 
  82                 String routeFilePath="/src/main/resources/dme2/preferredRoute.txt";
 
  84                 SimpleExamplePublisher publisher = new SimpleExamplePublisher();
 
  87                 File fo= new File(routeFilePath);
 
  89                                 routeWriter=new FileWriter(new File (routeFilePath));
 
  91                 routeReader= new FileReader(new File (routeFilePath));
 
  92                 props= new Properties();
 
  93                 publisher.publishMessage("/src/main/resources/dme2/producer.properties");