1 // xmlBlaster/demo/HelloWorld3.java
  2 import java.util.logging.Logger;
  3 import org.xmlBlaster.util.Global;
  4 import org.xmlBlaster.util.MsgUnit;
  5 import org.xmlBlaster.util.XmlBlasterException;
  6 import org.xmlBlaster.client.qos.ConnectQos;
  7 import org.xmlBlaster.client.qos.DisconnectQos;
  8 import org.xmlBlaster.client.I_Callback;
  9 import org.xmlBlaster.client.key.UpdateKey;
 10 import org.xmlBlaster.client.key.PublishKey;
 11 import org.xmlBlaster.client.key.GetKey;
 12 import org.xmlBlaster.client.key.SubscribeKey;
 13 import org.xmlBlaster.client.key.UnSubscribeKey;
 14 import org.xmlBlaster.client.key.EraseKey;
 15 import org.xmlBlaster.client.qos.GetQos;
 16 import org.xmlBlaster.client.qos.GetReturnQos;
 17 import org.xmlBlaster.client.qos.PublishQos;
 18 import org.xmlBlaster.client.qos.PublishReturnQos;
 19 import org.xmlBlaster.client.qos.UpdateQos;
 20 import org.xmlBlaster.client.qos.UpdateReturnQos;
 21 import org.xmlBlaster.client.qos.SubscribeQos;
 22 import org.xmlBlaster.client.qos.SubscribeReturnQos;
 23 import org.xmlBlaster.client.qos.EraseQos;
 24 import org.xmlBlaster.client.qos.EraseReturnQos;
 25 import org.xmlBlaster.client.qos.UnSubscribeQos;
 26 import org.xmlBlaster.client.qos.UnSubscribeReturnQos;
 27 import org.xmlBlaster.client.I_XmlBlasterAccess;
 28 
 29 
 30 /**
 31  * This client connects to xmlBlaster and invokes all available methods. 
 32  * <p />
 33  * We use java client helper classes to generate the raw xml strings, e.g.:
 34  * <pre>
 35  *   PublishKey pk = new PublishKey(glob, "HelloWorld3", "text/xml");
 36  * 
 37  * generates:
 38  *
 39  *   &lt;key oid='HelloWorld3' contentMime='text/xml'/>
 40  * </pre>
 41  *
 42  * Invoke: java HelloWorld3
 43  * <p />
 44  * Invoke: java HelloWorld3 -session.name joe -passwd secret
 45  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
 46  */
 47 public class HelloWorld3 implements I_Callback
 48 {
 49    private final Global glob;
 50    private static Logger log = Logger.getLogger(HelloWorld3.class.getName());
 51 
 52    public HelloWorld3(Global glob) {
 53       this.glob = glob;
 54 
 55       
 56       I_XmlBlasterAccess con = this.glob.getXmlBlasterAccess();
 57       
 58       try {
 59          // Check if other login name or password was given on command line:
 60          // (This is redundant as it is done by ConnectQos already)
 61          String name = glob.getProperty().get("session.name", "HelloWorld3");
 62          String passwd = glob.getProperty().get("passwd", "secret");
 63 
 64          ConnectQos qos = new ConnectQos(glob, name, passwd);
 65          con.connect(qos, this);  // Login to xmlBlaster, register for updates
 66 
 67 
 68          PublishKey pk = new PublishKey(glob, "HelloWorld3", "text/xml", "1.0");
 69          pk.setClientTags("<org.xmlBlaster><demo/></org.xmlBlaster>");
 70          PublishQos pq = new PublishQos(glob);
 71          pq.addClientProperty("myAge", 84);
 72          MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
 73          con.publish(msgUnit);
 74 
 75 
 76          GetKey gk = new GetKey(glob, "HelloWorld3");
 77          GetQos gq = new GetQos(glob);
 78          MsgUnit[] msgs = con.get(gk, gq);
 79          if (msgs.length > 0) {
 80             GetReturnQos grq = new GetReturnQos(glob, msgs[0].getQos());
 81             log.info("Accessed xmlBlaster message with content '" + new String(msgs[0].getContent()) +
 82                       "' and status=" + grq.getState());
 83          }
 84 
 85 
 86          SubscribeKey sk = new SubscribeKey(glob, "HelloWorld3");
 87          SubscribeQos sq = new SubscribeQos(glob);
 88          SubscribeReturnQos subRet = con.subscribe(sk, sq);
 89 
 90 
 91          msgUnit = new MsgUnit(pk, "Ho".getBytes(), pq);
 92          PublishReturnQos prq = con.publish(msgUnit);
 93 
 94          log.info("Got status='" + prq.getState() + "' for published message '" + prq.getKeyOid());
 95 
 96          try { Thread.sleep(1000); } 
 97          catch( InterruptedException i) {} // wait a second to receive update()
 98 
 99 
100          UnSubscribeKey uk = new UnSubscribeKey(glob, subRet.getSubscriptionId());
101          UnSubscribeQos uq = new UnSubscribeQos(glob);
102          UnSubscribeReturnQos[] urq = con.unSubscribe(uk, uq);
103          if (urq.length > 0) log.info("Unsubscribed from topic");
104 
105          EraseKey ek = new EraseKey(glob, "HelloWorld3");
106          EraseQos eq = new EraseQos(glob);
107          EraseReturnQos[] eraseArr = con.erase(ek, eq);
108          if (eraseArr.length > 0) log.info("Erased topic");
109 
110          DisconnectQos dq = new DisconnectQos(glob);
111          con.disconnect(dq);
112          glob.shutdown(); // free resources
113       }
114       catch (XmlBlasterException e) {
115          log.severe(e.getMessage());
116       }
117       catch (Throwable e) {
118          log.severe(e.toString());
119          e.printStackTrace();
120       }
121    }
122 
123    /**
124     * Implements I_Callback interface to receive asynchronously callback
125     * messages from xmlBlaster. 
126     */
127    public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
128                         UpdateQos updateQos)
129    {
130       if (updateKey.isInternal()) {
131          log.info("Received unexpected internal message '" +
132               updateKey.getOid() + " from xmlBlaster");
133          return "";
134       }
135 
136       int myAge = updateQos.getClientProperty("myAge", 0);
137       log.info("Received asynchronous message '" + updateKey.getOid() +
138                    "' state=" + updateQos.getState() +
139                    " content=" + new String(content) + 
140                    " clientProperty myAge=" + myAge + " from xmlBlaster");
141 
142       UpdateReturnQos uq = new UpdateReturnQos(glob);
143       return uq.toXml();
144    }
145 
146    /**
147     * Try
148     * <pre>
149     *   java HelloWorld3 -help
150     * </pre>
151     * for usage help
152     */
153    public static void main(String args[]) {
154       Global glob = new Global();
155       
156       if (glob.init(args) != 0) { // Get help with -help
157          System.out.println(glob.usage());
158          System.err.println("Example: java HelloWorld3 -session.name Jeff\n");
159          System.exit(1);
160       }
161 
162       new HelloWorld3(glob);
163    }
164 }


syntax highlighted by Code2HTML, v. 0.9.1