1 // xmlBlaster/demo/HelloWorld2.java
 2 import org.xmlBlaster.util.Global;
 3 import org.xmlBlaster.client.qos.ConnectQos;
 4 import org.xmlBlaster.client.I_Callback;
 5 import org.xmlBlaster.client.key.UpdateKey;
 6 import org.xmlBlaster.client.qos.UpdateQos;
 7 import org.xmlBlaster.client.I_XmlBlasterAccess;
 8 import org.xmlBlaster.util.MsgUnit;
 9 
10 
11 /**
12  * This client connects to xmlBlaster and subscribes to a message.
13  * <p />
14  * We then publish the message and receive it asynchronous in the update() method.
15  * <p />
16  * Invoke: java HelloWorld2
17  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
18  */
19 public class HelloWorld2 implements I_Callback
20 {
21    public HelloWorld2(final Global glob) {
22       try {
23          I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
24 
25          ConnectQos qos = new ConnectQos(glob);
26          con.connect(qos, this);  // Login to xmlBlaster, register for updates
27 
28          con.subscribe("<key oid='HelloWorld2'/>", "<qos/>");
29          
30          // A similar subscription with XPATH:
31          //con.subscribe("<key oid='' queryType='XPATH'>//key[@oid='HelloWorld2']</key>", "<qos/>");
32 
33          con.publish(new MsgUnit(glob, "<key oid='HelloWorld2'/>", "Hi".getBytes(),
34                                      "<qos/>"));
35 
36          // wait a second
37          try { Thread.sleep(1000); } catch(Exception e) { }
38          Global.waitOnKeyboardHit("\nHit a key to logout and terminate ...");
39 
40          con.erase("<key oid='HelloWorld2'/>", null);
41          try { Thread.sleep(100); } catch(Exception e) { }  // To process erase event
42          con.disconnect(null);
43       }
44       catch (Exception e) {
45          System.err.println(e.getMessage());
46       }
47    }
48 
49    public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
50                         UpdateQos updateQos)
51    {
52       System.out.println("\nHelloWorld: Received asynchronous message '" +
53          updateKey.getOid() + "' state=" + updateQos.getState() + " from xmlBlaster");
54       return "";
55    }
56 
57    /**
58     * Try
59     * <pre>
60     *   java HelloWorld2 -help
61     * </pre>
62     * for usage help
63     */
64    public static void main(String args[]) {
65       Global glob = new Global();
66 
67       if (glob.init(args) != 0) { // Get help with -help
68          System.out.println(glob.usage());
69          System.out.println("Example: java HelloWorld2 -session.name Jack");
70          System.exit(1);
71       }
72 
73       new HelloWorld2(glob);
74    }
75 }


syntax highlighted by Code2HTML, v. 0.9.1