1 // xmlBlaster/demo/javaclients/HelloWorldVolatile.java
  2 package javaclients;
  3 
  4 import java.util.logging.Logger;
  5 import java.util.logging.Level;
  6 import org.xmlBlaster.util.Global;
  7 import org.xmlBlaster.client.qos.ConnectQos;
  8 import org.xmlBlaster.client.qos.ConnectReturnQos;
  9 import org.xmlBlaster.client.qos.DisconnectQos;
 10 import org.xmlBlaster.util.MsgUnit;
 11 import org.xmlBlaster.util.XmlBlasterException;
 12 import org.xmlBlaster.client.I_Callback;
 13 import org.xmlBlaster.client.key.UpdateKey;
 14 import org.xmlBlaster.client.key.PublishKey;
 15 import org.xmlBlaster.client.key.GetKey;
 16 import org.xmlBlaster.client.key.SubscribeKey;
 17 import org.xmlBlaster.client.key.UnSubscribeKey;
 18 import org.xmlBlaster.client.key.EraseKey;
 19 import org.xmlBlaster.client.qos.GetQos;
 20 import org.xmlBlaster.client.qos.GetReturnQos;
 21 import org.xmlBlaster.client.qos.PublishQos;
 22 import org.xmlBlaster.client.qos.PublishReturnQos;
 23 import org.xmlBlaster.client.qos.UpdateQos;
 24 import org.xmlBlaster.client.qos.UpdateReturnQos;
 25 import org.xmlBlaster.client.qos.SubscribeQos;
 26 import org.xmlBlaster.client.qos.SubscribeReturnQos;
 27 import org.xmlBlaster.client.qos.EraseQos;
 28 import org.xmlBlaster.client.qos.EraseReturnQos;
 29 import org.xmlBlaster.client.qos.UnSubscribeQos;
 30 import org.xmlBlaster.client.I_XmlBlasterAccess;
 31 
 32 
 33 /**
 34  * This client connects to xmlBlaster and publishes a volatile message. 
 35  * <p>
 36  * Volatile messages are messages which expire instantly after they are received
 37  * by xmlBlaster. Subscribers which are there already will receive the message even
 38  * if the message is hanging in a clients callback queue for an hour.
 39  * The publish QoS settings for volatile messages are
 40  * <pre>
 41  * &lt;qos>
 42  *   &lt;expiration lifeTime='0' forceDestroy='true'/>
 43  * &lt;/qos>
 44  * </pre>
 45  * </p>
 46  * <p />
 47  * Invoke: java javaclients.HelloWorldVolatile
 48  * <p />
 49  * Invoke: java javaclients.HelloWorldVolatile -session.name joe -passwd secret
 50  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
 51  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.message.lifecycle.html" target="others">engine.message.lifecycle</a>
 52  */
 53 public class HelloWorldVolatile implements I_Callback
 54 {
 55    private final Global glob;
 56    private static Logger log = Logger.getLogger(HelloWorldVolatile.class.getName());
 57 
 58    public HelloWorldVolatile(Global glob) {
 59       this.glob = glob;
 60 
 61       try {
 62          I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
 63 
 64          ConnectQos qos = new ConnectQos(glob);
 65          con.connect(qos, this);  // Login to xmlBlaster, register for updates
 66 
 67          // Subscribe for the volatile message
 68          SubscribeKey sk = new SubscribeKey(glob, "HelloWorldVolatile");
 69          SubscribeQos sq = new SubscribeQos(glob);
 70          SubscribeReturnQos subRet = con.subscribe(sk, sq);
 71 
 72          // Publish a volatile message
 73          PublishKey pk = new PublishKey(glob, "HelloWorldVolatile", "text/xml", "1.0");
 74          PublishQos pq = new PublishQos(glob);
 75          pq.setVolatile(true);
 76          MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
 77          con.publish(msgUnit);
 78 
 79          // This should not be possible as the message was volatile
 80          try {
 81             GetKey gk = new GetKey(glob, "HelloWorldVolatile");
 82             GetQos gq = new GetQos(glob);
 83             MsgUnit[] msgs = con.get(gk, gq);
 84             if (msgs.length > 0) {
 85                GetReturnQos grq = new GetReturnQos(glob, msgs[0].getQos());
 86                log.severe("Did not expect any message as it was volatile");
 87             }
 88          }
 89          catch (XmlBlasterException e) {
 90             log.severe("Didn't expect an exception in get(): " + e.getMessage());
 91          }
 92 
 93          DisconnectQos dq = new DisconnectQos(glob);
 94          con.disconnect(dq);
 95       }
 96       catch (XmlBlasterException e) {
 97          log.severe(e.getMessage());
 98       }
 99    }
100 
101    public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
102                         UpdateQos updateQos) {
103       if (updateKey.isInternal()) {
104          log.info("Received unexpected internal message '" +
105               updateKey.getOid() + " from xmlBlaster");
106          return "";
107       }
108 
109       log.info("Received asynchronous message '" + updateKey.getOid() +
110                    "' state=" + updateQos.getState() +
111                    " content=" + new String(content) + " from xmlBlaster");
112 
113       UpdateReturnQos uq = new UpdateReturnQos(glob);
114       return uq.toXml();
115    }
116 
117    /**
118     * Try
119     * <pre>
120     *   java javaclients.HelloWorldVolatile -help
121     * </pre>
122     * for usage help
123     */
124    public static void main(String args[]) {
125       Global glob = new Global();
126       
127       if (glob.init(args) != 0) { // Get help with -help
128          System.out.println(glob.usage());
129          System.err.println("Example: java javaclients.HelloWorldVolatile -session.name Jeff\n");
130          System.exit(1);
131       }
132 
133       try {
134          new HelloWorldVolatile(glob);
135       }
136       catch (Throwable e) {
137          e.printStackTrace();
138          System.err.println("Unexpected problem: " + e.getMessage());
139       }
140    }
141 }


syntax highlighted by Code2HTML, v. 0.9.1