1 // xmlBlaster/demo/HelloWorld5.java
2 import java.util.logging.Logger;
3
4 import org.xmlBlaster.client.I_Callback;
5 import org.xmlBlaster.client.I_XmlBlasterAccess;
6 import org.xmlBlaster.client.key.PublishKey;
7 import org.xmlBlaster.client.key.UpdateKey;
8 import org.xmlBlaster.client.qos.ConnectQos;
9 import org.xmlBlaster.client.qos.ConnectReturnQos;
10 import org.xmlBlaster.client.qos.DisconnectQos;
11 import org.xmlBlaster.client.qos.PublishQos;
12 import org.xmlBlaster.client.qos.PublishReturnQos;
13 import org.xmlBlaster.client.qos.UpdateQos;
14 import org.xmlBlaster.util.Global;
15 import org.xmlBlaster.util.MsgUnit;
16 import org.xmlBlaster.util.SessionName;
17 import org.xmlBlaster.util.XmlBlasterException;
18 import org.xmlBlaster.util.qos.address.Destination;
19
20
21 /**
22 * A sender and a receiver client connect to xmlBlaster,
23 * the sender sends a PtP (point to point) message to the receiver
24 * and the receiver responds with an ACK message.
25 * <p />
26 * Invoke: java HelloWorld5
27 * <p />
28 * Note: This does not work with our 'email' protocol unless we configure
29 * two separate email accounts for each client connection.
30 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
31 */
32 public class HelloWorld5
33 {
34 private static Logger log = Logger.getLogger(HelloWorld5.class.getName());
35
36 private I_XmlBlasterAccess sender = null;
37 private final String senderName = "sender";
38 private I_XmlBlasterAccess receiver = null;
39 private final String receiverName = "receiver";
40
41 public HelloWorld5(final Global glob) {
42
43 try {
44
45 { // setup the sender client ...
46 sender = glob.getXmlBlasterAccess();
47
48 ConnectQos qos = new ConnectQos(sender.getGlobal(), senderName, "secret");
49 ConnectReturnQos conRetQos = sender.connect(qos, new I_Callback() {
50 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
51 log.info("Receiving asynchronous message '" + updateKey.getOid() + "' in sender default handler" );
52 log.info("Received: " + updateKey.toXml() + "\n <content>" + new String(content) + "</content>" + updateQos.toXml());
53 return "";
54 }
55 }); // Login to xmlBlaster, default handler for updates
56
57 log.info("Sender connected to xmlBlaster " + conRetQos.getSessionName().getRelativeName());
58 }
59
60
61 { // setup the receiver client ...
62 Global globReceiver = glob.getClone(null);
63 receiver = globReceiver.getXmlBlasterAccess();
64
65 ConnectQos qos = new ConnectQos(receiver.getGlobal(), receiverName, "secret");
66 ConnectReturnQos conRetQos = receiver.connect(qos, new I_Callback() {
67 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
68 log.info("Receiving asynchronous message '" + updateKey.getOid() + "' in receiver default handler");
69 log.info("Received: " + updateKey.toXml() + "\n <content>" + new String(content) + "</content>" + updateQos.toXml());
70
71 if (updateKey.isInternal()) return "";
72 if (updateQos.isErased()) return "";
73 try {
74 // Send an ACK back ...
75 PublishKey pk = new PublishKey(receiver.getGlobal(), "HelloWorld5:ACK", "text/plain", "1.0");
76 PublishQos pq = new PublishQos(receiver.getGlobal());
77 pq.addDestination(new Destination(updateQos.getSender()));
78 MsgUnit msgUnit = new MsgUnit(pk, "ACK", pq);
79 boolean oneway = false; // just for demo, you can try a variant with never blocking oneway
80 if (oneway) {
81 MsgUnit[] arr = new MsgUnit[1];
82 arr[0] = msgUnit;
83 receiver.publishOneway(arr);
84 log.info("Published message '" + pk.getOid() + "' to " + updateQos.getSender());
85 }
86 else {
87 PublishReturnQos retQos = receiver.publish(msgUnit);
88 log.info("Published message '" + retQos.getKeyOid() + "' to " + updateQos.getSender());
89 }
90 }
91 catch (XmlBlasterException e) {
92 log.severe("Sending ACK to " + updateQos.getSender() + " failed: " + e.getMessage());
93 }
94 return "";
95 }
96 }); // Login to xmlBlaster, default handler for updates
97
98 log.info("Receiver connected to xmlBlaster " + conRetQos.getSessionName().getRelativeName());
99 }
100
101 // Send a message to 'receiver'
102 PublishKey pk = new PublishKey(sender.getGlobal(), "HelloWorld5", "text/plain", "1.0");
103 PublishQos pq = new PublishQos(sender.getGlobal());
104 pq.addDestination(new Destination(new SessionName(sender.getGlobal(), receiverName)));
105 MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
106 PublishReturnQos retQos = sender.publish(msgUnit);
107 log.info("Published message '" + retQos.getKeyOid() + "' to " + receiverName + ":\n" + msgUnit.toXml());
108 }
109 catch (XmlBlasterException e) {
110 log.severe("Houston, we have a problem: " + e.getMessage());
111 }
112 finally {
113 // Wait a second for messages to arrive before we logout
114 try { Thread.sleep(1000); } catch( InterruptedException i) {}
115 Global.waitOnKeyboardHit("Success, hit a key to exit");
116
117 if (sender != null && sender.isConnected()) { sender.disconnect(new DisconnectQos(sender.getGlobal())); }
118 if (receiver != null && receiver.isConnected()) { receiver.disconnect(new DisconnectQos(receiver.getGlobal())); }
119 }
120 }
121
122 /**
123 * Try
124 * <pre>
125 * java HelloWorld5 -help
126 * </pre>
127 * for usage help
128 */
129 public static void main(String args[]) {
130 Global glob = new Global();
131
132 if (glob.init(args) != 0) { // Get help with -help
133 System.out.println(glob.usage());
134 System.out.println("HelloWorld5: Example: java HelloWorld5\n");
135 System.exit(1);
136 }
137
138 new HelloWorld5(glob);
139 }
140 }
syntax highlighted by Code2HTML, v. 0.9.1