1 /*------------------------------------------------------------------------------
  2 Name:      ClientXml.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo code for a client using xmlBlaster
  6 Version:   $Id: ClientXml.java 14861 2006-03-07 19:19:47Z goetzger $
  7 ------------------------------------------------------------------------------*/
  8 package javaclients;
  9 
 10 import org.xmlBlaster.util.Global;
 11 import org.xmlBlaster.util.StopWatch;
 12 import java.util.logging.Logger;
 13 import org.xmlBlaster.client.qos.ConnectQos;
 14 import org.xmlBlaster.client.I_XmlBlasterAccess;
 15 import org.xmlBlaster.client.I_Callback;
 16 import org.xmlBlaster.client.key.UpdateKey;
 17 import org.xmlBlaster.client.qos.UpdateQos;
 18 import org.xmlBlaster.util.XmlBlasterException;
 19 import org.xmlBlaster.util.MsgUnit;
 20 
 21 
 22 /**
 23  * This client tests the method subscribe()/publish() with XML syntax key
 24  * and XPath query using the DefaultCallback implementation.
 25  * <p>
 26  * It is a nice example using the DefaultCallback implementation from I_XmlBlasterAccess.java
 27  * which calls the update() method using I_Callback interface when messages arrive.
 28  * <p>
 29  * Have a look into the testsuite for other possibilities.
 30  * <p>
 31  * Invoke examples:<br />
 32  * <pre>
 33  *    java javaclients.ClientXml
 34  *
 35  *    java javaclients.ClientXml -loginName "Jeff"
 36  * </pre>
 37  */
 38 public class ClientXml implements I_Callback
 39 {
 40    private static String ME = "ClientXml";
 41    private static Logger log = Logger.getLogger(ClientXml.class.getName());
 42 
 43    public ClientXml(String args[])
 44    {
 45       // Initialize command line argument handling (this is optional)
 46       Global glob = new Global();
 47 
 48       if (glob.init(args) != 0) {
 49          System.out.println(glob.usage());
 50          log.info("Example: java javaclients.ClientXml -loginName Jeff\n");
 51          System.exit(1);
 52       }
 53 
 54       StopWatch stop = new StopWatch();
 55       try {
 56          I_XmlBlasterAccess blasterConnection = glob.getXmlBlasterAccess();
 57 
 58          // Login and install the Callback server
 59          ConnectQos qos = new ConnectQos(glob);
 60          blasterConnection.connect(qos, this);
 61 
 62 
 63          String publishOid = "";
 64          String xmlKey = "<key oid='' contentMime='text/xml'>\n" +
 65                          "<AGENT id='192.168.124.10' subId='1' type='generic'>" +
 66                          "<DRIVER id='FileProof' pollingFreq='10'>" +
 67                          "</DRIVER>"+
 68                          "</AGENT>" +
 69                          "</key>";
 70 
 71 
 72          //----------- Construct a message and publish it ---------
 73          {
 74             String content = "<person><name>Ghandi</name></person>";
 75             MsgUnit msgUnit = new MsgUnit(xmlKey, content.getBytes(), "");
 76             log.fine("Publishing ...");
 77             stop.restart();
 78             try {
 79                publishOid = blasterConnection.publish(msgUnit).getKeyOid();
 80                log.info("   Returned oid=" + publishOid);
 81                log.fine("Publishing done" + stop.nice());
 82             } catch(XmlBlasterException e) {
 83                log.severe("Punlishing failed, XmlBlasterException: " + e.getMessage());
 84             }
 85          }
 86 
 87 
 88          //----------- Subscribe to the previous message OID -------
 89          log.fine("Subscribing using the exact oid ...");
 90          xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
 91                   "<key oid='" + publishOid + "' queryType='EXACT'>\n" +
 92                   "</key>";
 93          stop.restart();
 94          try {
 95             String subId = blasterConnection.subscribe(xmlKey, "<qos></qos>").getSubscriptionId();
 96             log.fine("Subscribed to '" + subId + "' ..." + stop.nice());
 97          } catch(XmlBlasterException e) {
 98             log.severe("Subscribe failed, XmlBlasterException: " + e.getMessage());
 99          }
100 
101          try { Thread.currentThread().sleep(2000); } catch( InterruptedException i) {} // Wait a second
102 
103 
104          //----------- Unsubscribe from the previous message --------
105          log.fine("Unsubscribe ...");
106          stop.restart();
107          try {
108             blasterConnection.unSubscribe(xmlKey, "<qos></qos>");
109             log.info("Unsubscribe done" + stop.nice());
110          } catch(XmlBlasterException e) {
111             log.severe("Unsubscribe failed, XmlBlasterException: " + e.getMessage());
112          }
113 
114 
115          //----------- Subscribe to the previous message XPATH -------
116          log.fine("Subscribing using XPath syntax ...");
117          xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
118                   "<key oid='' queryType='XPATH'>\n" +
119                   "/xmlBlaster/key/AGENT" +
120                   "</key>";
121          stop.restart();
122          try {
123             blasterConnection.subscribe(xmlKey, "<qos></qos>");
124             log.fine("Subscribe done, there should be a Callback");
125          } catch(XmlBlasterException e) {
126             log.severe("subscribe failed, XmlBlasterException: " + e.getMessage());
127          }
128 
129          try { Thread.currentThread().sleep(2000); } catch( InterruptedException i) {} // Wait a second
130 
131          log.fine("Publishing 10 times ...");
132          {
133             for (int ii=0; ii<10; ii++) {
134                //----------- Construct a message and publish it ---------
135                String content = "<person><name>Castro</name><age>" + ii + "</age></person>";
136                xmlKey = "<key oid='" + publishOid + "' contentMime='text/xml'>\n</key>";
137                MsgUnit msgUnit = new MsgUnit(xmlKey, content.getBytes(), "");
138                log.fine("Publishing ...");
139                stop.restart();
140                try {
141                   String str = blasterConnection.publish(msgUnit).getKeyOid();
142                   log.fine("Publishing done" + stop.nice());
143                } catch(XmlBlasterException e) {
144                   log.severe("Publishing failed, XmlBlasterException: " + e.getMessage());
145                }
146             }
147          }
148 
149          try { Thread.currentThread().sleep(2000); } catch( InterruptedException i) {} // Wait a second
150          blasterConnection.disconnect(null);
151       }
152       catch (Exception e) {
153           e.printStackTrace();
154       }
155 
156       // blasterConnection.getOrb().run(); // Usually your client won't exit after this, uncomment the run() method
157    }
158 
159    /**
160     * This is the callback method invoked from xmlBlaster
161     * delivering us a new asynchronous message. 
162     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
163     */
164    public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
165    {
166       log.info("Receiving update of message [" + updateKey.getOid() + "]");
167       return "";
168    }
169 
170    public static void main(String args[])
171    {
172       new ClientXml(args);
173    }
174 }


syntax highlighted by Code2HTML, v. 0.9.1