1 // xmlBlaster/demo/javaclients/Latency.java
2 package javaclients;
3
4 import java.util.logging.Logger;
5 import java.util.logging.Level;
6 import org.xmlBlaster.util.*;
7 import org.xmlBlaster.client.I_Callback;
8 import org.xmlBlaster.client.key.SubscribeKey;
9 import org.xmlBlaster.client.key.PublishKey;
10 import org.xmlBlaster.client.key.UpdateKey;
11 import org.xmlBlaster.client.qos.ConnectQos;
12 import org.xmlBlaster.client.qos.DisconnectQos;
13 import org.xmlBlaster.client.qos.PublishQos;
14 import org.xmlBlaster.client.qos.SubscribeQos;
15 import org.xmlBlaster.client.qos.UpdateQos;
16 import org.xmlBlaster.client.I_XmlBlasterAccess;
17 import org.xmlBlaster.util.MsgUnit;
18
19
20 /**
21 * Measure the brutto roundtrip latency of a message publish and update.
22 * <p />
23 * Invoke examples (put xmlBlaster.jar in your CLASSPATH):
24 * <br />
25 * On localhost:
26 * <pre>
27 * java org.xmlBlaster.Main
28 * java javaclients.Latency -numSend 100
29 * </pre>
30 * <br />
31 * Over the internet with CORBA:
32 * <pre>
33 * java javaclients.Latency -numSend 100 -bootstrapHostname server.xmlBlaster.org
34 * or if you have a dynamic IP:
35 * java javaclients.Latency -numSend 100 -bootstrapHostname server.xmlBlaster.org -dispatch/callback/plugin/ior/hostname <myCurrentIP>
36 * </pre>
37 * <br />
38 * Over the internet with XmlRpc:
39 * <pre>
40 * java javaclients.Latency -numSend 100 -dispatch/connection/protocol XMLRPC -dispatch/connection/plugin/xmlrpc/hostname server.xmlBlaster.org -dispatch/callback/plugin/xmlrpc/hostname <myCurrentIP>
41 * </pre>
42 * <p />
43 * Results, for one round trip including publish -> processing in xmlBlaster -> update -> parsing in client (600 MHz AMD Linux):
44 * <br />
45 * <ul>
46 * <li>CORBA in intranet: ~ 6 milliseconds</li>
47 * <li>XmlRpc in intranet: ~ 16 milliseconds</li>
48 * <li>CORBA over internet: ~ 105 milliseconds</li>
49 * <li>XmlRpc over internet: ~ 320 milliseconds</li>
50 * </ul>
51 */
52 public class Latency implements I_Callback
53 {
54 private long startTime = 0L;
55 private boolean messageArrived = false;
56 private static Logger log = Logger.getLogger(Latency.class.getName());
57
58 public Latency(Global glob) {
59
60 try {
61 I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
62
63 ConnectQos qos = new ConnectQos(glob);
64 con.connect(qos, this); // Login to xmlBlaster, register for updates
65
66 PublishKey pk = new PublishKey(glob, "Latency", "text/xml", "1.0");
67 PublishQos pq = new PublishQos(glob);
68 MsgUnit msgUnit = new MsgUnit(pk.toXml(), "Hi".getBytes(), pq.toXml());
69
70 SubscribeKey sk = new SubscribeKey(glob, "Latency");
71 SubscribeQos sq = new SubscribeQos(glob);
72 con.subscribe(sk.toXml(), sq.toXml()).getSubscriptionId();
73
74 int numSend = glob.getProperty().get("numSend", 10);
75
76 for (int ii=0; ii<numSend; ii++) {
77 startTime = System.currentTimeMillis();
78 con.publish(msgUnit);
79 while (true) {
80 try { Thread.sleep(50); } catch( InterruptedException i) {}
81 if (messageArrived) {
82 messageArrived = false;
83 break;
84 }
85 }
86 }
87
88 try { Thread.sleep(1000); } catch( InterruptedException i) {} // wait a second to receive update()
89
90 DisconnectQos dq = new DisconnectQos(glob);
91 con.disconnect(dq);
92 }
93 catch (Exception e) {
94 log.severe(e.toString());
95 }
96 }
97
98 public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
99 UpdateQos updateQos)
100 {
101 long endTime = System.currentTimeMillis();
102 messageArrived = true;
103 log.info("Received asynchronous message '" + updateKey.getOid() +
104 "' from xmlBlaster - latency=" + (endTime - startTime) + " millis");
105 return "";
106 }
107
108 /**
109 * Try
110 * <pre>
111 * java javaclients.Latency -help
112 * </pre>
113 * for usage help
114 */
115 public static void main(String args[]) {
116 Global glob = new Global();
117
118 if (glob.init(args) != 0) { // Get help with -help
119 System.out.println(glob.usage());
120 Global.instance().usage();
121 System.err.println("Example: java javaclients.Latency -loginName Jeff\n");
122 System.exit(1);
123 }
124
125 new Latency(glob);
126 }
127 }
syntax highlighted by Code2HTML, v. 0.9.1