1 // xmlBlaster/demo/HelloWorld6.java
2 import java.util.logging.Level;
3 import java.util.logging.Logger;
4
5 import org.xmlBlaster.client.I_Callback;
6 import org.xmlBlaster.client.I_ConnectionStateListener;
7 import org.xmlBlaster.client.I_XmlBlasterAccess;
8 import org.xmlBlaster.client.key.EraseKey;
9 import org.xmlBlaster.client.key.PublishKey;
10 import org.xmlBlaster.client.key.SubscribeKey;
11 import org.xmlBlaster.client.key.UpdateKey;
12 import org.xmlBlaster.client.qos.ConnectQos;
13 import org.xmlBlaster.client.qos.ConnectReturnQos;
14 import org.xmlBlaster.client.qos.DisconnectQos;
15 import org.xmlBlaster.client.qos.EraseQos;
16 import org.xmlBlaster.client.qos.EraseReturnQos;
17 import org.xmlBlaster.client.qos.PublishQos;
18 import org.xmlBlaster.client.qos.PublishReturnQos;
19 import org.xmlBlaster.client.qos.SubscribeQos;
20 import org.xmlBlaster.client.qos.SubscribeReturnQos;
21 import org.xmlBlaster.client.qos.UpdateQos;
22 import org.xmlBlaster.util.Global;
23 import org.xmlBlaster.util.MsgUnit;
24 import org.xmlBlaster.util.XmlBlasterException;
25 import org.xmlBlaster.util.dispatch.ConnectionStateEnum;
26 import org.xmlBlaster.util.qos.address.Address;
27 import org.xmlBlaster.util.qos.address.CallbackAddress;
28 import org.xmlBlaster.util.qos.storage.ClientQueueProperty;
29
30
31 /**
32 * This client connects to xmlBlaster in fail save mode and uses specific update handlers.
33 * <p />
34 * In fail save mode the client will poll for the xmlBlaster server and
35 * queue messages until the server is available.<br />
36 * Further you see how to configure the connection behavior hard coded.
37 * <p />
38 * Invoke: java HelloWorld6 -session.name jack/5
39 * <p />
40 * Invoke: java HelloWorld6 -session.name joe/2 -passwd secret -dispatch/connection/protocol XMLRPC
41 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
42 */
43 public class HelloWorld6
44 {
45 private static Logger log = Logger.getLogger(HelloWorld6.class.getName());
46 private I_XmlBlasterAccess con = null;
47 private ConnectReturnQos conRetQos = null;
48
49 public HelloWorld6(final Global glob) {
50
51
52
53 try {
54 con = glob.getXmlBlasterAccess();
55
56 /*
57 // Change hard-coded the protocol and server lookup:
58 String[] args = { "-protocol", "SOCKET",
59 "-dispatch/connection/plugin/socket/hostname", "server.xmlBlaster.org",
60 "-dispatch/connection/plugin/socket/port", "9455",
61 //"-dispatch/connection/plugin/socket/localHostname", "myHost.com",
62 //"-dispatch/connection/plugin/socket/localPort", "8888"
63 };
64 glob.init(args);
65 */
66
67 ConnectQos connectQos = new ConnectQos(glob);
68
69 ClientQueueProperty prop = new ClientQueueProperty(glob, null);
70 prop.setMaxEntries(10000); // Client side queue up to 10000 entries if not connected
71
72 Address address = new Address(glob);
73 address.setDelay(4000L); // retry connecting every 4 sec
74 address.setRetries(-1); // -1 == forever
75 address.setPingInterval(2000L); // ping every 2 sec
76
77 // Example how to hardcode a XmlRpc server:
78 //address.setType("XMLRPC"); // force XmlRpc protocol
79 //address.setRawAddress("http://noty:9456/"); // Address to find the server
80
81 // Example how to hardcode a SOCKET server:
82 //address.setType("SOCKET"); // force SOCKET protocol
83 //address.setRawAddress("socket://noty:9988"); // Address to find the server
84
85 prop.setAddress(address);
86 connectQos.addClientQueueProperty(prop);
87
88 CallbackAddress cbAddress = new CallbackAddress(glob);
89 cbAddress.setDelay(4000L); // retry connecting every 4 sec
90 cbAddress.setRetries(-1); // -1 == forever
91 cbAddress.setPingInterval(4000L); // ping every 4 seconds
92 //cbAddress.setDispatcherActive(false);
93
94 // Example how to hardcode a SOCKET server:
95 //cbAddress.setType("SOCKET"); // force SOCKET protocol for callback
96
97 connectQos.addCallbackAddress(cbAddress);
98
99 // We want to be notified about connection states:
100 con.registerConnectionListener(new I_ConnectionStateListener() {
101
102 public void reachedAlive(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
103 conRetQos = connection.getConnectReturnQos();
104 log.info("I_ConnectionStateListener: We were lucky, connected to " + glob.getId() + " as " + conRetQos.getSessionName());
105 // we can access the queue via connectionHandler and for example erase the entries ...
106 }
107 public void reachedPolling(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
108 log.warning("I_ConnectionStateListener: No connection to " + glob.getId() + ", we are polling ...");
109 }
110 public void reachedDead(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
111 log.warning("I_ConnectionStateListener: Connection to " + glob.getId() + " is DEAD -> Good bye");
112 System.exit(1);
113 }
114 });
115
116 // We connect to xmlBlaster and register the callback handle:
117 this.conRetQos = con.connect(connectQos, new I_Callback() {
118
119 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
120 if (log.isLoggable(Level.FINEST)) log.finest("UpdateKey.toString()=" + updateKey.toString());
121 if (updateKey.isInternal()) {
122 log.severe("Receiving unexpected asynchronous internal message '" + updateKey.getOid() +
123 "' in default handler");
124 return "";
125 }
126 if (updateQos.isErased()) {
127 log.info("Message '" + updateKey.getOid() + "' is erased");
128 return "";
129 }
130 if (updateKey.getOid().equals("Banking"))
131 log.info("Receiving asynchronous message '" + updateKey.getOid() +
132 "' state=" + updateQos.getState() + " in default handler");
133 else
134 log.severe("Receiving unexpected asynchronous message '" + updateKey.getOid() +
135 "' in default handler");
136 return "";
137 }
138
139 }); // Login to xmlBlaster, default handler for updates
140
141
142 if (con.isAlive())
143 log.info("Connected as " + connectQos.getUserId() + " to xmlBlaster: " + this.conRetQos.getSessionName());
144 else
145 log.info("Not connected to xmlBlaster, proceeding in fail save mode ...");
146
147
148 SubscribeKey sk = new SubscribeKey(glob, "Banking");
149 SubscribeQos sq = new SubscribeQos(glob);
150 SubscribeReturnQos sr1 = con.subscribe(sk, sq);
151 log.info("Subsrcibed with id " + sr1.getSubscriptionId());
152
153
154 sk = new SubscribeKey(glob, "HelloWorld6");
155 sq = new SubscribeQos(glob);
156 SubscribeReturnQos sr2 = con.subscribe(sk, sq, new I_Callback() {
157 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
158 if (updateKey.getOid().equals("HelloWorld6"))
159 log.info("Receiving asynchronous message '" + updateKey.getOid() +
160 "' state=" + updateQos.getState() + " in HelloWorld6 handler");
161 else
162 log.severe("Receiving unexpected asynchronous message '" + updateKey.getOid() +
163 "' with state '" + updateQos.getState() + "' in HelloWorld6 handler");
164 return "";
165 }
166 }); // subscribe with our specific update handler
167 log.info("Subsrcibed with id " + sr2.getSubscriptionId());
168
169
170 PublishKey pk = new PublishKey(glob, "HelloWorld6", "text/plain", "1.0");
171 PublishQos pq = new PublishQos(glob);
172 MsgUnit msgUnit = new MsgUnit(pk, "Hi".getBytes(), pq);
173 PublishReturnQos retQos = con.publish(msgUnit);
174 log.info("Published message '" + pk.getOid() + "' " + retQos.getState());
175
176
177 pk = new PublishKey(glob, "Banking", "text/plain", "1.0");
178 pk.setClientTags("<Account><withdraw/></Account>"); // Add banking specific meta data
179 pq = new PublishQos(glob);
180 msgUnit = new MsgUnit(pk, "Ho".getBytes(), pq);
181 con.publish(msgUnit);
182 log.info("Published message '" + pk.getOid() + "'");
183 }
184 catch (XmlBlasterException e) {
185 log.severe("Houston, we have a problem: " + e.toString());
186 }
187 finally {
188 // Wait a second for messages to arrive before we logout
189 try { Thread.sleep(1000); } catch( InterruptedException i) {}
190 Global.waitOnKeyboardHit("Success, hit a key to exit");
191
192 if (con != null && con.isConnected()) {
193 try {
194 EraseQos eq = new EraseQos(glob);
195
196 EraseKey ek = new EraseKey(glob, "HelloWorld6");
197 con.erase(ek, eq);
198
199 ek = new EraseKey(glob, "Banking");
200 EraseReturnQos[] er = con.erase(ek, eq);
201 if (er.length > 0)
202 log.info("Eased topic '" + er[0].getKeyOid() + "'");
203
204 // Wait on message erase events
205 try { Thread.sleep(1000); } catch( InterruptedException i) {}
206 }
207 catch (XmlBlasterException e) {
208 log.severe("Houston, we have a problem: " + e.toString());
209 e.printStackTrace();
210 }
211
212 con.disconnect(new DisconnectQos(glob));
213 }
214 }
215 }
216
217 /**
218 * Try
219 * <pre>
220 * java HelloWorld6 -help
221 * </pre>
222 * for usage help
223 */
224 public static void main(String args[]) {
225 Global glob = new Global();
226
227 if (glob.init(args) != 0) { // Get help with -help
228 System.out.println(glob.usage());
229 System.err.println("Example: java HelloWorld6 -session.name Jeff\n");
230 System.exit(1);
231 }
232
233 new HelloWorld6(glob);
234 }
235 }
syntax highlighted by Code2HTML, v. 0.9.1