1 // xmlBlaster/demo/HelloWorld7.java
2 import java.util.logging.Logger;
3 import org.xmlBlaster.util.Global;
4 import org.xmlBlaster.util.XmlBlasterException;
5 import org.xmlBlaster.util.def.Constants;
6 import org.xmlBlaster.client.qos.ConnectQos;
7 import org.xmlBlaster.client.I_Callback;
8 import org.xmlBlaster.client.key.UpdateKey;
9 import org.xmlBlaster.client.qos.UpdateQos;
10 //import org.xmlBlaster.client.qos.UpdateReturnQos;
11 import org.xmlBlaster.client.I_XmlBlasterAccess;
12
13
14 /**
15 * This is a dumb client, it does almost nothing.
16 * It initializes the xmlBlaster client library
17 * and the email callback server but does not connect to xmlBlaster. This is useful
18 * when the client can't reach the xmlBlaster server because of a firewall or the like.
19 * <br />
20 * Another 'delegate' client, for example started as a native xmlBlaster plugin,
21 * logs in with the same name and does for example all subscribes - as a delegate.
22 * It then just disappears without disconnecting - thus leaving the session alive.
23 * Arriving messages will then be routed to our dumb HelloWorld7.
24 * <p />
25 * Invoke:
26 * <pre>
27 * # 1. Start the server and setup an email MTA as described in the protocol.email requirement
28 * ...
29 *
30 * # 2. Start our dumb email client, it will wait for messages
31 * java HelloWorld7 -protocol email -session.name joe/1 -dispatch/connection/pingInterval 0
32 *
33 * # 3.Start a delegate, hit enter to subscribe and than kill it with Ctrl-C (to not disconnect):
34 * java javaclients.HelloWorldSubscribe -protocol email -session.name subscribe/1 -persistentSubscribe true -dispatch/callback/pingInterval 5000 -dispatch/callback/retries -1 -dispatch/connection/protocol XMLRPC
35 *
36 * # 4. Start a publisher and watch the messages arriving at HelloWorld7
37 * java javaclients.HelloWorldPublish -numPublish 10
38 * </pre>
39 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
40 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.email.html" target="others">The protocol.email requirement</a>
41 */
42 public class HelloWorld7 implements I_Callback
43 {
44 private final Global glob;
45 private static Logger log = Logger.getLogger(HelloWorld7.class.getName());
46
47 public HelloWorld7(Global glob) {
48 this.glob = glob;
49
50
51 I_XmlBlasterAccess con = this.glob.getXmlBlasterAccess();
52
53 try {
54 ConnectQos qos = new ConnectQos(glob);
55
56 // '-dispatch/connection/doSendConnect false' on command line would do the same
57 qos.doSendConnect(false);
58
59 // Initializes everything but does NOT send connect message
60 con.connect(qos, this);
61
62 log.info("Waiting now for updates ...");
63 char ret = 0;
64 while (ret != 'q')
65 ret = (char)Global.waitOnKeyboardHit("Enter 'q' to quit");
66
67 con.disconnect(null); // Cleanup client library
68 }
69 catch (XmlBlasterException e) {
70 log.severe(e.getMessage());
71 }
72 catch (Throwable e) {
73 log.severe(e.toString());
74 e.printStackTrace();
75 }
76 }
77
78 public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
79 UpdateQos updateQos)
80 {
81 if (updateKey.isInternal()) {
82 log.info("Received unexpected internal message '" +
83 updateKey.getOid() + " from xmlBlaster");
84 return "";
85 }
86
87 int myAge = updateQos.getClientProperty("myAge", 0);
88 log.info("Received asynchronous message '" + updateKey.getOid() +
89 "' state=" + updateQos.getState() +
90 " content=" + new String(content) +
91 " clientProperty myAge=" + myAge + " from xmlBlaster");
92
93 return Constants.RET_OK;
94 //UpdateReturnQos uq = new UpdateReturnQos(glob);
95 //return uq.toXml();
96 }
97
98 /**
99 * Try
100 * <pre>
101 * java HelloWorld7 -help
102 * </pre>
103 * for usage help
104 */
105 public static void main(String args[]) {
106 Global glob = new Global();
107
108 if (glob.init(args) != 0) { // Get help with -help
109 System.out.println(glob.usage());
110 System.err.println("Example: java HelloWorld7 -session.name Jeff\n");
111 System.exit(1);
112 }
113
114 new HelloWorld7(glob);
115 }
116 }
syntax highlighted by Code2HTML, v. 0.9.1