1 /*------------------------------------------------------------------------------
2 Name: ClientSub.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Demo code for a client using xmlBlaster
6 Version: $Id: ClientSub.java 14813 2006-03-04 23:02:48Z laghi $
7 ------------------------------------------------------------------------------*/
8 package javaclients;
9
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12 import org.xmlBlaster.util.Global;
13 import org.xmlBlaster.util.XmlBlasterException;
14 import org.xmlBlaster.client.I_XmlBlasterAccess;
15 import org.xmlBlaster.client.I_Callback;
16 import org.xmlBlaster.client.key.UpdateKey;
17 import org.xmlBlaster.client.qos.UpdateQos;
18 import org.xmlBlaster.client.qos.PublishReturnQos;
19 import org.xmlBlaster.client.qos.EraseReturnQos;
20 import org.xmlBlaster.client.key.SubscribeKey;
21 import org.xmlBlaster.client.qos.SubscribeQos;
22 import org.xmlBlaster.util.MsgUnit;
23
24
25 /**
26 * This client tests the method subscribe() with a later publish() with XPath query.<br />
27 * The subscribe() should be recognized for this later arriving publish().
28 * <p>
29 * This demo uses the I_XmlBlasterAccess helper class, which hides the raw
30 * CORBA/RMI/XMLRPC nastiness.<br />
31 * I_XmlBlasterAccesss hides how to find the xmlBlaster server (see I_XmlBlasterAccess API).<br />
32 * I_XmlBlasterAccess installs a callback server (for CORBA,RMI or XMLRPC) for you and informs
33 * you about asynchronous callbacks using the I_Callback interface (method update() see below).
34 * <p>
35 * If you want to know step by step what happens with CORBA, study the corba/ClientRaw.java example.
36 * Here we use all available Java helper classes.
37 * <p>
38 * Invoke examples:<br />
39 * <pre>
40 * java -cp ../../lib/xmlBlaster.jar javaclients.ClientSub
41 *
42 * java javaclients.ClientSub -session.name Jeff -dispatch/connection/protocol RMI
43 *
44 * java javaclients.ClientSub -help
45 * </pre>
46 */
47 public class ClientSub implements I_Callback
48 {
49 private static String ME = "ClientSub";
50 private final Global glob;
51 private static Logger log = Logger.getLogger(ClientSub.class.getName());
52 private int numReceived = 0; // error checking
53 public static long startTime;
54 public static long elapsed;
55
56 public ClientSub(Global glob) {
57 this.glob = glob;
58
59 try {
60 I_XmlBlasterAccess blasterConnection = glob.getXmlBlasterAccess();
61 blasterConnection.connect(null, this);
62 // Now we are connected to xmlBlaster MOM server.
63
64 int numTests = glob.getProperty().get("numTests", 1);
65 for (int i=0; i<numTests; i++)
66 sendSomeMessages(blasterConnection);
67
68 /* // Run forever
69 while (true) {
70 try { Thread.currentThread().sleep(100000000L);
71 } catch(InterruptedException e) { log.warning("Caught exception: " + e.toString()); }
72 }
73 */
74
75 blasterConnection.disconnect(null);
76 }
77 catch (Exception e) {
78 log.severe("Client failed: " + e.toString());
79 // e.printStackTrace();
80 }
81 }
82
83 private void sendSomeMessages(I_XmlBlasterAccess blasterConnection)
84 {
85 String subscriptionId="";
86 try {
87 // Subscribe to messages with XPATH using some helper classes
88 {
89 log.info("Subscribing using XPath syntax ...");
90
91 // SubscribeKey helps us to create this string:
92 // "<key oid='' queryType='XPATH'>" +
93 // " /xmlBlaster/key/ClientSub-AGENT" +
94 // "</key>";
95 SubscribeKey key = new SubscribeKey(glob, "/xmlBlaster/key/ClientSub-AGENT", "XPATH");
96
97 // SubscribeKey helps us to create "<qos></qos>":
98 SubscribeQos qos = new SubscribeQos(glob);
99
100 try {
101 subscriptionId = blasterConnection.subscribe(key.toXml(), qos.toXml()).getSubscriptionId();
102 log.info("Subscribe done, there should be no Callback, subcriptionId=" + subscriptionId);
103 } catch(XmlBlasterException e) {
104 log.warning("XmlBlasterException: " + e.getMessage());
105 }
106 }
107
108 try { Thread.sleep(1000); } catch( InterruptedException i) {} // Wait a second
109
110 if (numReceived == 0)
111 log.info("Success, no Callback for a simple subscribe without a publish");
112 else
113 log.severe("Got Callback, but didn't expect one after a simple subscribe without a publish");
114 numReceived = 0;
115
116
117 //----------- Construct a message and publish it ---------
118 PublishReturnQos pubRetQos = null;
119 {
120 // This time, as an example, we don't use the wrapper helper classes,
121 // and create the string 'by hand':
122 String xmlKey = // optional: "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
123 "<key oid='' contentMime='text/xml'>\n" +
124 " <ClientSub-AGENT id='192.168.124.10' subId='1' type='generic'>" +
125 " <ClientSub-DRIVER id='FileProof' pollingFreq='10'>" +
126 " </ClientSub-DRIVER>"+
127 " </ClientSub-AGENT>" +
128 "</key>";
129 String content = "Yeahh, i'm the new content";
130 MsgUnit msgUnit = new MsgUnit(xmlKey, content.getBytes(), "<qos></qos>");
131 log.info("Publishing ...");
132 try {
133 startTime = System.currentTimeMillis();
134 pubRetQos = blasterConnection.publish(msgUnit);
135 log.info("Publishing done, returned oid=" + pubRetQos.getKeyOid());
136 } catch(XmlBlasterException e) {
137 log.severe("XmlBlasterException: " + e.getMessage());
138 System.exit(1);
139 }
140 }
141
142 try { Thread.sleep(1000); } catch( InterruptedException i) {} // Wait a second
143
144 if (numReceived == 1)
145 log.info("Success, got Callback after publishing");
146 else
147 log.severe(numReceived + " callbacks arrived, did expect one after a simple subscribe with a publish");
148 numReceived = 0;
149
150 log.info("Hit a key to exit");
151 try { System.in.read(); } catch(java.io.IOException e) {}
152
153 //----------- cleaning up .... unSubscribe() the previous message OID -------
154 {
155 String xmlKey = "<key oid='" + subscriptionId + "'/>";
156 String qos = "<qos></qos>";
157 numReceived = 0;
158 try {
159 blasterConnection.unSubscribe(xmlKey, qos);
160 log.info("Success: UnSubscribe with " + subscriptionId + " done");
161 } catch(XmlBlasterException e) {
162 log.warning("XmlBlasterException: " + e.getMessage());
163 }
164 }
165
166
167 //----------- cleaning up .... erase() the previous message OID -------
168 {
169 String xmlKey = "<key oid='" + pubRetQos.getKeyOid() + "' queryType='EXACT'/>";
170 try {
171 EraseReturnQos[] strArr = blasterConnection.erase(xmlKey, "<qos></qos>");
172 if (strArr.length != 1) log.severe("Erased " + strArr.length + " messages:");
173 } catch(XmlBlasterException e) { log.severe("XmlBlasterException: " + e.getMessage()); }
174 }
175 }
176 catch (Exception e) {
177 log.severe("Client failed: " + e.toString());
178 //e.printStackTrace();
179 }
180 }
181
182
183 /**
184 * This is the callback method invoked from xmlBlaster
185 * delivering us a new asynchronous message.
186 *
187 * @param cbSessionId The session ID specified by the client which registered the callback
188 * @param updateKey The arrived key
189 * @param content The arrived message content
190 * @param qos Quality of Service of the MsgUnit
191 *
192 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
193 */
194 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
195 {
196 elapsed = System.currentTimeMillis() - startTime;
197 numReceived++;
198 log.info("Received asynchronous callback-update " + numReceived + " with cbSessionId='" + cbSessionId + "' from xmlBlaster from publisher " + updateQos.getSender() + " (latency=" + elapsed + " milli seconds):");
199 System.out.println(updateKey.toXml());
200 System.out.println((new String(content)).toString());
201 System.out.println(updateQos.toXml());
202 return "";
203 }
204
205 public static void main(String args[]) {
206 Global glob = new Global();
207 if (glob.init(args) != 0) {
208 System.out.println(glob.usage());
209 System.out.println("Get help: java javaclients.ClientSub -help\n");
210 System.out.println("Example: java javaclients.ClientSub -session.name Jeff\n");
211 System.exit(1);
212 }
213 new ClientSub(glob);
214 }
215 } // ClientSub
syntax highlighted by Code2HTML, v. 0.9.1