1 // xmlBlaster/demo/HelloWorld9.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_XmlBlasterAccess;
 9 
10 
11 /**
12  * This client connects to xmlBlaster and blocks on a receive() call. 
13  * <p />
14  * Please invoke a publisher first to test it.
15  * <pre>
16  * Invoke:
17  *   java org.xmlBlaster.Main 
18  *   java javaclients.HelloWorldPublish -numPublish 10
19  *   java HelloWorld9
20  * </pre>
21  * 
22  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
23  */
24 public class HelloWorld9
25 {
26    private final Global glob;
27    private static Logger log = Logger.getLogger(HelloWorld9.class.getName());
28    private boolean stopThis;
29    private long timeout = 60000; // Avoid unlimited = -1 to prevent a potential thread leak
30    private boolean consuming = true;
31    private String queueOid;
32 
33    public HelloWorld9(Global glob) {
34       this.glob = glob;
35       final I_XmlBlasterAccess con = this.glob.getXmlBlasterAccess();
36       this.timeout = this.glob.getProperty().get("timeout", timeout);
37       this.consuming = this.glob.getProperty().get("consuming", consuming);
38       this.queueOid = this.glob.getProperty().get("queueOid", "topic/Hello");
39       
40       try {
41          ConnectQos qos = new ConnectQos(glob);
42          con.connect(qos, null);  // Login to xmlBlaster
43          
44          Thread thread = new Thread(new Runnable() {
45             public void run() {
46                while (true) {
47                   try {
48                      int ret = Global.waitOnKeyboardHit("Hit a key to call receive() or 'q' to quit> ");
49                      if (ret == 'q') {
50                         stopThis = true;
51                         break;
52                      }
53                      int count = 1;
54                      if (ret > '0' && ret <= '9') count = ret - '0';
55                      System.out.println("Waiting on '" + queueOid + "' maxEntries=" + count + " timeout=" + timeout + " consumable=" + consuming + " ...");
56                      MsgUnit[] msgs = con.receive(queueOid, count, timeout, consuming);
57                      System.out.println("Received " + msgs.length + " messages");
58                      for (int i=0; i<msgs.length; i++)
59                         System.out.println("#" + i + ": " + msgs[i].getContentStr());
60                   }
61                   catch (XmlBlasterException e) {
62                      log.severe(e.getMessage());
63                   }
64                }
65             }
66          });
67          thread.start();
68 
69          while (!stopThis)
70             try { Thread.sleep(100); } catch (InterruptedException e) {}
71 
72          con.disconnect(new DisconnectQos(glob));
73          glob.shutdown(); // free resources
74       }
75       catch (XmlBlasterException e) {
76          log.severe(e.getMessage());
77       }
78    }
79 
80    /**
81     * Try
82     * <pre>
83     *   java HelloWorld9 -help
84     * </pre>
85     * for usage help
86     */
87    public static void main(String args[]) {
88       Global glob = new Global();
89       
90       if (glob.init(args) != 0) { // Get help with -help
91          System.out.println(glob.usage());
92          System.err.println("Example: java HelloWorld9 -session.name Jeff\n");
93          System.exit(1);
94       }
95 
96       new HelloWorld9(glob);
97    }
98 }


syntax highlighted by Code2HTML, v. 0.9.1