/* * * Copyright (c) 1999 - 2011 my-Channels Ltd * Copyright (c) 2012 - 2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors. * * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG. * */ package com.pcbsys.nirvana.apps; import org.apache.xerces.parsers.*; import org.w3c.dom.Document; import org.xml.sax.InputSource; import com.pcbsys.foundation.utils.xmlHelper; import com.pcbsys.nirvana.client.*; import java.io.*; /** * Publishes an XML DOM document to a nirvana channel */ public class XMLPublish extends nSampleApp { private Document doc; private static XMLPublish mySelf = null; /** * This method demonstrates the Nirvana API calls necessary to publish an * XMl document to a channel. It is called after all command line arguments * have been received and validated * * @param realmDetails a String[] containing the possible RNAME values * @param achannelName the channel / queue name to publish to * @param count the number of messages to publish * @param filename xml file to be published */ private void doit(String[] realmDetails, String achannelName, int count, String filename) { mySelf.constructSession(realmDetails); // Publishes to the specified channel try { // Create a channel attributes object nChannelAttributes nca = new nChannelAttributes(); nca.setName(achannelName); nChannel myChannel = null; nQueue myQueue = null; long startEid = 0; try { // Obtain a reference to the channel myChannel = mySession.findChannel(nca); startEid = myChannel.getLastEID(); } catch (nIllegalChannelMode isChannel) { // Obtain a reference to the queue myQueue = mySession.findQueue(nca); } // Load the file containing the XML doc File aFile = new File(filename); if (!aFile.exists()) { System.out.println("File : " + filename + " does not exist !"); System.exit(1); } // Create the XML Document try { // Crate an input stream InputStream is = new FileInputStream(aFile); // Create a DOM Parser object DOMParser p = new DOMParser(); // Parse from the input stream p.parse(new InputSource(is)); // Get the XML Document doc = p.getDocument(); } catch (Exception e) { e.printStackTrace(); } // Create an XML Helper object help us calculate the size in bytes xmlHelper xh = new xmlHelper(doc); // Inform the user that publishing is about to start System.out.println("Starting publish of " + count + " events of size " + xh.encode().length); // Get a timestamp to be used to calculate the message publishing // rates long start = System.currentTimeMillis(); // Loop as many times as the number of messages we want to publish for (int x = 0; x < count; x++) { if (myChannel != null) { // Publish the event to the channel myChannel.publish("XML-" + x, doc); // Note that the XML tag // is not needed } else { // Publish the event to the queue myQueue.push("XML-" + x, doc); // Note that the XML tag is // not needed } } if (myChannel != null) { // Calculate the actual number of events published by obtaining // the channel's last eid after our publishing and subtracting // the channel's last eid before our publishing. // This also ensures that all client queues have been flushed. long events = myChannel.getLastEID() - startEid; // Get a timestamp to calculate the publishing rates long end = System.currentTimeMillis(); // Calculate the events / sec rate long eventPerSec = (((events) * 1000) / ((end + 1) - start)); // Calculate the bytes / sec rate long bytesPerSec = eventPerSec * (xh.encode().length); // Inform the user of the resulting rates System.out.println("Events = " + events + " Events/sec = " + eventPerSec + " Bytes/Sec = " + bytesPerSec); } } // Handle errors catch (nChannelNotFoundException cnfe) { System.out.println("The channel specified could not be found."); System.out.println("Please ensure that the channel exists in the REALM you connect to."); cnfe.printStackTrace(); System.exit(1); } catch (nSecurityException se) { System.out.println("Insufficient permissions for the requested operation."); System.out.println("Please check the ACL settings on the server."); se.printStackTrace(); System.exit(1); } catch (nSessionNotConnectedException snce) { System.out.println("The session object used is not physically connected to the Nirvana realm."); System.out.println("Please ensure the realm is up and check your RNAME value."); snce.printStackTrace(); System.exit(1); } catch (nUnexpectedResponseException ure) { System.out.println("The Nirvana REALM has returned an unexpected response."); System.out.println("Please ensure the Nirvana REALM and client API used are compatible."); ure.printStackTrace(); System.exit(1); } catch (nUnknownRemoteRealmException urre) { System.out.println("The channel specified resided in a remote realm which could not be found."); System.out.println("Please ensure the channel name specified is correct."); urre.printStackTrace(); System.exit(1); } catch (nRequestTimedOutException rtoe) { System.out.println("The requested operation has timed out waiting for a response from the REALM."); System.out.println("If this is a very busy REALM ask your administrator to increase the client timeout values."); rtoe.printStackTrace(); System.exit(1); } catch (nBaseClientException nbce) { System.out.println("An error occured while creating the Channel Attributes object."); nbce.printStackTrace(); System.exit(1); } // Close the session we opened try { nSessionFactory.close(mySession); } catch (Exception ex) { } // Close any other sessions within this JVM so that we can exit nSessionFactory.shutdown(); } protected void processArgs(String[] args) { switch (args.length) { case 3: System.getProperties().put("COUNT", args[2]); case 2: System.getProperties().put("FILENAME", args[1]); case 1: if (args[0].equals("-?")) { UsageEnv(); } System.getProperties().put("CHANNAME", args[0]); break; } } public static void main(String[] args) { // Create an instance for this class mySelf = new XMLPublish(); // Process command line arguments mySelf.processArgs(args); // Process Environment Variables nSampleApp.processEnvironmentVariables(); // Check the channel name specified String channelName = null; if (System.getProperty("CHANNAME") != null) { channelName = System.getProperty("CHANNAME"); } else { Usage(); System.exit(1); } int count = 10; // default value // Check if the number of messages to be published has been specified if (System.getProperty("COUNT") != null) { try { count = Integer.parseInt(System.getProperty("COUNT")); } catch (Exception num) { } // Ignore and use the defaults } // Check for the file containing the XML document to be published String fileName = System.getProperty("FILENAME"); if (fileName == null) { System.out.println("No file name supplied"); Usage(); System.exit(1); } // Check the local realm details String RNAME = null; if (System.getProperty("RNAME") != null) { RNAME = System.getProperty("RNAME"); } else { Usage(); System.exit(1); } // Process the local REALM RNAME details String[] rproperties = new String[4]; rproperties = parseRealmProperties(RNAME); // Publish to the channel specified mySelf.doit(rproperties, channelName, count, fileName); } /** * Prints the usage message for this class */ private static void Usage() { System.out.println("Usage ...\n"); System.out.println("nxmlpub [count] [size] \n"); System.out.println(" \n"); System.out.println(" - Channel name parameter for the channel to publish to"); System.out.println(" - The full path of the xml file to publish "); System.out.println("\n[Optional Arguments] \n"); System.out.println("[count] -The number of events to publish (default: 10)"); System.out.println("\n\nNote: -? provides help on environment variables \n"); } } // End of XMLPublish Class