1 /*------------------------------------------------------------------------------
  2 Name:      ClientSubDispatch.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: ClientSubDispatch.java 14813 2006-03-04 23:02:48Z laghi $
  7 ------------------------------------------------------------------------------*/
  8 package javaclients;
  9 
 10 import java.util.logging.Logger;
 11 import java.util.logging.Level;
 12 import org.xmlBlaster.util.Global;
 13 import org.xmlBlaster.client.qos.ConnectQos;
 14 import org.xmlBlaster.util.XmlBlasterException;
 15 import org.xmlBlaster.client.I_XmlBlasterAccess;
 16 import org.xmlBlaster.client.I_Callback;
 17 import org.xmlBlaster.client.key.UpdateKey;
 18 import org.xmlBlaster.client.qos.UpdateQos;
 19 import org.xmlBlaster.client.qos.EraseReturnQos;
 20 import org.xmlBlaster.client.key.SubscribeKey;
 21 import org.xmlBlaster.client.qos.SubscribeQos;
 22 import org.xmlBlaster.util.MsgUnit;
 23 
 24 
 25 /**
 26  * This client demonstrates the method subscribe() with a later publish().
 27  * <p />
 28  * We use a subscribe variant, where for every subscribe we define a
 29  * specialized update method.<br />
 30  * Like this not all callback messages arrive in a centralized update()
 31  * with the need to look into them and decide why the arrived.
 32  * <p />
 33  * This demo uses the I_XmlBlasterAccess helper class, which hides the raw
 34  * CORBA/RMI/XMLRPC nastiness and allows this client side dispatching.
 35  * <br />
 36  * Invoke examples:<br />
 37  * <pre>
 38  *    java -cp ../../lib/xmlBlaster.jar javaclients.ClientSubDispatch
 39  *
 40  *    java javaclients.ClientSubDispatch -loginName Jeff -dispatch/connection/protocol RMI
 41  *
 42  *    java javaclients.ClientSubDispatch -help
 43  * </pre>
 44  */
 45 public class ClientSubDispatch implements I_Callback
 46 {
 47    private static String ME = "ClientSubDispatch";
 48    private final Global glob;
 49    private static Logger log = Logger.getLogger(ClientSubDispatch.class.getName());
 50    private int numReceived1 = 0;         // error checking
 51    private int numReceived2 = 0;         // error checking
 52 
 53    public ClientSubDispatch(Global glob) {
 54       this.glob = glob;
 55 
 56 
 57       try {
 58          ConnectQos loginQos = new ConnectQos(null); // creates "<qos></qos>" string
 59          I_XmlBlasterAccess blasterConnection = glob.getXmlBlasterAccess();
 60          blasterConnection.connect(loginQos, this);  // Now we are connected to xmlBlaster MOM server.
 61 
 62          // Subscribe to messages with XPATH using some helper classes
 63          log.info("Subscribing #1 for anonymous callback class using XPath syntax ...");
 64          SubscribeKey key = new SubscribeKey(glob, "//DispatchTest", "XPATH");
 65          SubscribeQos qos = new SubscribeQos(glob);
 66          blasterConnection.subscribe(key, qos, new I_Callback() {
 67                public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 68                   log.info("Receiving message with specialized update() #1 ...");
 69                   numReceived1++;
 70                   System.out.println(updateKey.toXml());
 71                   System.out.println((new String(content)).toString());
 72                   System.out.println(updateQos.toXml());
 73                   return "";
 74                }
 75             });
 76 
 77 
 78          log.info("Subscribing #2 for anonymous callback class using XPath syntax ...");
 79          key = new SubscribeKey(glob, "A message id");
 80          blasterConnection.subscribe(key, qos, new I_Callback() {
 81                public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 82                   log.info("Receiving message with specialized update() #2 ...");
 83                   numReceived2++;
 84                   System.out.println(updateKey.toXml());
 85                   System.out.println((new String(content)).toString());
 86                   System.out.println(updateQos.toXml());
 87                   return "";
 88                }
 89             });
 90 
 91 
 92          // Construct a message and publish it ...
 93          String publishOid1 = "";
 94          // This time, as an example, we don't use the wrapper helper classes,
 95          // and create the string 'by hand':
 96          String xmlKey =   "<key oid='' contentMime='text/xml'>\n" +
 97                            "   <DispatchTest>" +
 98                            "   </DispatchTest>" +
 99                            "</key>";
100          String content = "Some content #1";
101          MsgUnit msgUnit = new MsgUnit(xmlKey, content.getBytes(), "<qos></qos>");
102          publishOid1 = blasterConnection.publish(msgUnit).getKeyOid();
103          log.info("Publishing done, returned oid=" + publishOid1);
104 
105          try { Thread.sleep(1000); } catch( InterruptedException i) {} // Wait a second
106 
107          String publishOid2 = "";
108          xmlKey = "<key oid='A message id' contentMime='text/xml'>\n" +
109                   "</key>";
110          content = "Some content #2";
111          msgUnit = new MsgUnit(xmlKey, content.getBytes(), "<qos></qos>");
112          publishOid2 = blasterConnection.publish(msgUnit).getKeyOid();
113          log.info("Publishing done, returned oid=" + publishOid2);
114 
115 
116          try { Thread.sleep(1000); } catch( InterruptedException i) {} // Wait a second
117 
118          if (numReceived1 == 1)
119             log.info("Success, got Callback #1 after publishing");
120          else
121             log.severe(numReceived1 + " callbacks arrived, did expect one after a simple subscribe with a publish");
122 
123          if (numReceived2 == 1)
124             log.info("Success, got Callback #2 after publishing");
125          else
126             log.severe(numReceived2 + " callbacks arrived, did expect one after a simple subscribe with a publish");
127 
128 
129          // cleaning up .... erase() the previous published message
130          xmlKey = "<key oid='" + publishOid1 + "' queryType='EXACT'>\n" +
131                   "</key>";
132          EraseReturnQos[] strArr = blasterConnection.erase(xmlKey, "<qos></qos>");
133          if (strArr.length != 1) log.severe("Erased " + strArr.length + " message.");
134 
135          xmlKey = "<key oid='" + publishOid2 + "' queryType='EXACT'>\n" +
136                   "</key>";
137          strArr = blasterConnection.erase(xmlKey, "<qos></qos>");
138          if (strArr.length != 1) log.severe("Erased " + strArr.length + " message.");
139 
140          blasterConnection.disconnect(null);
141       }
142       catch(XmlBlasterException e) {
143          log.severe("XmlBlasterException: " + e.getMessage());
144       }
145       catch (Exception e) {
146          log.severe("Client failed: " + e.toString());
147          e.printStackTrace();
148       }
149    }
150 
151    /**
152     * This is the callback method invoked from xmlBlaster
153     * delivering us a new asynchronous message. 
154     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
155     */
156    public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
157    {
158       log.severe("Received unexpected asynchronous callback-update from xmlBlaster from publisher " + updateQos.getSender() + ":");
159       log.severe(updateKey.toXml() + "\n" + updateQos.toXml());
160       return "";
161    }
162 
163    public static void main(String args[]) {
164       new ClientSubDispatch(new Global(args));
165    }
166 } // ClientSubDispatch


syntax highlighted by Code2HTML, v. 0.9.1