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 public void reachedAliveSync(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
115 }
116
117 });
118
119 // We connect to xmlBlaster and register the callback handle:
120 this.conRetQos = con.connect(connectQos, new I_Callback() {
121
122 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
123 if (log.isLoggable(Level.FINEST)) log.finest("UpdateKey.toString()=" + updateKey.toString());
124 if (updateKey.isInternal()) {
125 log.severe("Receiving unexpected asynchronous internal message '" + updateKey.getOid() +
126 "' in default handler");
127 return "";
128 }
129 if (updateQos.isErased()) {
130 log.info("Message '" + updateKey.getOid() + "' is erased");
131 return "";
132 }
133 if (updateKey.getOid().equals("Banking"))
134 log.info("Receiving asynchronous message '" + updateKey.getOid() +
135 "' state=" + updateQos.getState() + " in default handler");
136 else
137 log.severe("Receiving unexpected asynchronous message '" + updateKey.getOid() +
138 "' in default handler");
139 return "";
140 }
141
142 }); // Login to xmlBlaster, default handler for updates
143
144
145 if (con.isAlive())
146 log.info("Connected as " + connectQos.getUserId() + " to xmlBlaster: " + this.conRetQos.getSessionName());
147 else
148 log.info("Not connected to xmlBlaster, proceeding in fail save mode ...");
149
150
151 SubscribeKey sk = new SubscribeKey(glob, "Banking");
152 SubscribeQos sq = new SubscribeQos(glob);
153 SubscribeReturnQos sr1 = con.subscribe(sk, sq);
154 log.info("Subsrcibed with id " + sr1.getSubscriptionId());
155
156
157 sk = new SubscribeKey(glob, "HelloWorld6");
158 sq = new SubscribeQos(glob);
159 SubscribeReturnQos sr2 = con.subscribe(sk, sq, new I_Callback() {
160 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
161 if (updateKey.getOid().equals("HelloWorld6"))
162 log.info("Receiving asynchronous message '" + updateKey.getOid() +
163 "' state=" + updateQos.getState() + " in HelloWorld6 handler");
164 else
165 log.severe("Receiving unexpected asynchronous message '" + updateKey.getOid() +
166 "' with state '" + updateQos.getState() + "' in HelloWorld6 handler");
167 return "";
168 }
169 }); // subscribe with our specific update handler
170 log.info("Subsrcibed with id " + sr2.getSubscriptionId());
171
172
173 PublishKey pk = new PublishKey(glob, "HelloWorld6", "text/plain", "1.0");
174 PublishQos pq = new PublishQos(glob);
175 MsgUnit msgUnit = new MsgUnit(pk, "Hi".getBytes(), pq);
176 PublishReturnQos retQos = con.publish(msgUnit);
177 log.info("Published message '" + pk.getOid() + "' " + retQos.getState());
178
179
180 pk = new PublishKey(glob, "Banking", "text/plain", "1.0");
181 pk.setClientTags("<Account><withdraw/></Account>"); // Add banking specific meta data
182 pq = new PublishQos(glob);
183 msgUnit = new MsgUnit(pk, "Ho".getBytes(), pq);
184 con.publish(msgUnit);
185 log.info("Published message '" + pk.getOid() + "'");
186 }
187 catch (XmlBlasterException e) {
188 log.severe("Houston, we have a problem: " + e.toString());
189 }
190 finally {
191 // Wait a second for messages to arrive before we logout
192 try { Thread.sleep(1000); } catch( InterruptedException i) {}
193 Global.waitOnKeyboardHit("Success, hit a key to exit");
194
195 if (con != null && con.isConnected()) {
196 try {
197 EraseQos eq = new EraseQos(glob);
198
199 EraseKey ek = new EraseKey(glob, "HelloWorld6");
200 con.erase(ek, eq);
201
202 ek = new EraseKey(glob, "Banking");
203 EraseReturnQos[] er = con.erase(ek, eq);
204 if (er.length > 0)
205 log.info("Eased topic '" + er[0].getKeyOid() + "'");
206
207 // Wait on message erase events
208 try { Thread.sleep(1000); } catch( InterruptedException i) {}
209 }
210 catch (XmlBlasterException e) {
211 log.severe("Houston, we have a problem: " + e.toString());
212 e.printStackTrace();
213 }
214
215 con.disconnect(new DisconnectQos(glob));
216 }
217 }
218 }
219
220 /**
221 * Try
222 * <pre>
223 * java HelloWorld6 -help
224 * </pre>
225 * for usage help
226 */
227 public static void main(String args[]) {
228 Global glob = new Global();
229
230 if (glob.init(args) != 0) { // Get help with -help
231 System.out.println(glob.usage());
232 System.err.println("Example: java HelloWorld6 -session.name Jeff\n");
233 System.exit(1);
234 }
235
236 new HelloWorld6(glob);
237 }
238 }
syntax highlighted by Code2HTML, v. 0.9.1