1 // xmlBlaster/demo/HelloWorld8.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.def.Constants;
 19 import org.xmlBlaster.util.qos.address.Destination;
 20 
 21 
 22 /**
 23  * Shows the request/reply pattern. 
 24  * <p />
 25  * A sender and a receiver client connect to xmlBlaster,
 26  * the sender sends a request message to the receiver and blocks until a reply arrives.
 27  * The receiver responds with a reply message.
 28  * <p />
 29  * Invoke: java HelloWorld8
 30  * <p />
 31  * Note: This does not work with our 'email' protocol unless we configure
 32  *       two separate email accounts for each client connection.
 33  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
 34  */
 35 public class HelloWorld8
 36 {
 37    private static Logger log = Logger.getLogger(HelloWorld8.class.getName());
 38 
 39    private I_XmlBlasterAccess sender = null;
 40    private final String senderName = "TheDesperate";
 41    private I_XmlBlasterAccess receiver = null;
 42    private final String receiverName = "TheKnowing";
 43 
 44    public HelloWorld8(final Global glob) {
 45       
 46       try {
 47 
 48          {  // setup the sender client ...
 49             sender = glob.getXmlBlasterAccess();
 50 
 51             ConnectQos qos = new ConnectQos(sender.getGlobal(), senderName, "secret");
 52             ConnectReturnQos conRetQos = sender.connect(qos, null); // Login to xmlBlaster
 53 
 54             log.info("Sender connected to xmlBlaster " + conRetQos.getSessionName().getRelativeName());
 55          }
 56 
 57 
 58          {  // setup the receiver client which processes the request (usually another process) ...
 59             Global globReceiver = glob.getClone(null);
 60             receiver = globReceiver.getXmlBlasterAccess();
 61 
 62             ConnectQos qos = new ConnectQos(receiver.getGlobal(), receiverName, "secret");
 63             ConnectReturnQos conRetQos = receiver.connect(qos, new I_Callback() {
 64                public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 65                   log.info(receiverName+": Receiving asynchronous message '" + updateKey.getOid() + "' in receiver default handler");
 66                   log.info(receiverName+": Received: " + updateKey.toXml() + "\n <content>" + new String(content) + "</content>" + updateQos.toXml());
 67 
 68                   if (updateKey.isInternal()) return "";
 69                   if (updateQos.isErased()) return "";
 70                   try {
 71                      String tempTopicOid = updateQos.getClientProperty(Constants.JMS_REPLY_TO, ""); // __jms:JMSReplyTo
 72                      log.info(receiverName+": Got request, using topic '" + tempTopicOid + "' for response");
 73 
 74                      // Send reply back ...
 75                      PublishKey pk = new PublishKey(receiver.getGlobal(), tempTopicOid, "text/plain", "1.0");
 76                      PublishQos pq = new PublishQos(receiver.getGlobal());
 77                      MsgUnit msgUnit = new MsgUnit(pk, "On doubt no ultimate truth, my dear.", pq);
 78                      //try { Thread.sleep(8000); } catch (InterruptedException e) { e.printStackTrace(); }
 79                      PublishReturnQos retQos = receiver.publish(msgUnit);
 80                      log.info(receiverName+": Published reply message using temporary topic " + retQos.getKeyOid());
 81                   }
 82                   catch (XmlBlasterException e) {
 83                      log.severe(receiverName+": Sending reply to " + updateQos.getSender() + " failed: " + e.getMessage());
 84                   }
 85                   return "";
 86                }
 87             });  // Login to xmlBlaster, default handler for updates
 88 
 89             log.info("Receiver connected to xmlBlaster " + conRetQos.getSessionName().getRelativeName());
 90          }
 91 
 92          // Send a message to 'receiver' and block for the reply
 93          PublishKey pk = new PublishKey(sender.getGlobal(), "requestForEnlightenment");
 94          PublishQos pq = new PublishQos(sender.getGlobal());
 95          pq.addDestination(new Destination(new SessionName(sender.getGlobal(), receiverName)));
 96          MsgUnit msgUnit = new MsgUnit(pk, "Tell me the truth!", pq);
 97          MsgUnit[] replies = sender.request(msgUnit, 6000, 1);
 98          if (replies.length > 0)
 99             log.info(senderName+": Got " + replies.length + " reply :\n" + replies[0].toXml());
100          else
101             log.severe(senderName+": Missing reply message.");
102       }
103       catch (XmlBlasterException e) {
104          log.severe("We have a problem: " + e.getMessage());
105       }
106       finally {
107          try { Thread.sleep(1000); } catch( InterruptedException i) {}
108          Global.waitOnKeyboardHit("Success, hit a key to exit");
109          
110          if (sender != null && sender.isConnected()) { sender.disconnect(new DisconnectQos(sender.getGlobal())); }
111          if (receiver != null && receiver.isConnected()) { receiver.disconnect(new DisconnectQos(receiver.getGlobal())); }
112       }
113    }
114 
115    /**
116     * Try
117     * <pre>
118     *   java HelloWorld8 -help
119     * </pre>
120     * for usage help
121     */
122    public static void main(String args[]) {
123       Global glob = new Global();
124       
125       if (glob.init(args) != 0) { // Get help with -help
126          System.out.println(glob.usage());
127          System.out.println("Example: java HelloWorld8\n");
128          System.exit(1);
129       }
130 
131       new HelloWorld8(glob);
132    }
133 }


syntax highlighted by Code2HTML, v. 0.9.1