1 // xmlBlaster/demo/javaclients/HelloWorldVolatile2.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.util.qos.TopicProperty;
13 import org.xmlBlaster.client.I_Callback;
14 import org.xmlBlaster.client.key.UpdateKey;
15 import org.xmlBlaster.client.key.PublishKey;
16 import org.xmlBlaster.client.key.GetKey;
17 import org.xmlBlaster.client.key.SubscribeKey;
18 import org.xmlBlaster.client.key.UnSubscribeKey;
19 import org.xmlBlaster.client.key.EraseKey;
20 import org.xmlBlaster.client.qos.GetQos;
21 import org.xmlBlaster.client.qos.GetReturnQos;
22 import org.xmlBlaster.client.qos.PublishQos;
23 import org.xmlBlaster.client.qos.PublishReturnQos;
24 import org.xmlBlaster.client.qos.UpdateQos;
25 import org.xmlBlaster.client.qos.UpdateReturnQos;
26 import org.xmlBlaster.client.qos.SubscribeQos;
27 import org.xmlBlaster.client.qos.SubscribeReturnQos;
28 import org.xmlBlaster.client.qos.EraseQos;
29 import org.xmlBlaster.client.qos.EraseReturnQos;
30 import org.xmlBlaster.client.qos.UnSubscribeQos;
31 import org.xmlBlaster.client.I_XmlBlasterAccess;
32
33
34 /**
35 * This client connects to xmlBlaster and publishes a volatile message.
36 * <p>
37 * Additionally the topic destroyDelay is reduced to 4 seconds, we can
38 * see in the xmlBlaster dump that the topic disappeared 4 seconds later.
39 * <pre>
40 * <qos>
41 * <expiration lifeTime='0' forceDestroy='true'/>
42 * </qos>
43 * </pre>
44 * </p>
45 * <p />
46 * Invoke: java javaclients.HelloWorldVolatile2
47 * <p />
48 * Invoke: java javaclients.HelloWorldVolatile2 -session.name joe -passwd secret
49 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
50 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.message.lifecycle.html" target="others">engine.message.lifecycle</a>
51 */
52 public class HelloWorldVolatile2 implements I_Callback
53 {
54 private final Global glob;
55 private static Logger log = Logger.getLogger(HelloWorldVolatile2.class.getName());
56
57 public HelloWorldVolatile2(Global glob) {
58 this.glob = glob;
59
60 try {
61 I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
62
63 // Check if other name or password was given on command line:
64 String name = glob.getProperty().get("session.name", "HelloWorldVolatile2");
65 String passwd = glob.getProperty().get("passwd", "secret");
66
67 ConnectQos connectQos = new ConnectQos(glob, name, passwd);
68 con.connect(connectQos, this); // Login to xmlBlaster, register for updates
69
70 // Subscribe for the volatile message
71 SubscribeKey sk = new SubscribeKey(glob, "HelloWorldVolatile2");
72 SubscribeQos sq = new SubscribeQos(glob);
73 SubscribeReturnQos subRet = con.subscribe(sk, sq);
74
75 // Publish a volatile message
76 PublishKey pk = new PublishKey(glob, "HelloWorldVolatile2", "text/xml", "1.0");
77 PublishQos pq = new PublishQos(glob);
78 pq.setVolatile(true);
79 // Configure the topic to our needs
80 TopicProperty topicProperty = new TopicProperty(glob);
81 topicProperty.setDestroyDelay(4000L);
82 topicProperty.setCreateDomEntry(false);
83 pq.setTopicProperty(topicProperty);
84 MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
85 con.publish(msgUnit);
86
87 // This should not be possible as the message was volatile
88 try {
89 GetKey gk = new GetKey(glob, "HelloWorldVolatile2");
90 GetQos gq = new GetQos(glob);
91 MsgUnit[] msgs = con.get(gk, gq);
92 if (msgs.length > 0) {
93 GetReturnQos grq = new GetReturnQos(glob, msgs[0].getQos());
94 log.severe("Did not expect any message as it was volatile");
95 }
96 }
97 catch (XmlBlasterException e) {
98 log.severe("Didn't expect an exception in get(): " + e.getMessage());
99 }
100
101 DisconnectQos dq = new DisconnectQos(glob);
102 con.disconnect(dq);
103 }
104 catch (XmlBlasterException e) {
105 log.severe(e.getMessage());
106 }
107 }
108
109 public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
110 UpdateQos updateQos) {
111 if (updateKey.isInternal()) {
112 log.info("Received unexpected internal message '" +
113 updateKey.getOid() + " from xmlBlaster");
114 return "";
115 }
116
117 log.info("Received asynchronous message '" + updateKey.getOid() +
118 "' state=" + updateQos.getState() +
119 " content=" + new String(content) + " from xmlBlaster");
120
121 UpdateReturnQos uq = new UpdateReturnQos(glob);
122 return uq.toXml();
123 }
124
125 /**
126 * Try
127 * <pre>
128 * java javaclients.HelloWorldVolatile2 -help
129 * </pre>
130 * for usage help
131 */
132 public static void main(String args[]) {
133 Global glob = new Global();
134
135 if (glob.init(args) != 0) { // Get help with -help
136 System.out.println(glob.usage());
137 System.err.println("Example: java javaclients.HelloWorldVolatile2 -session.name Jeff\n");
138 System.exit(1);
139 }
140
141 try {
142 new HelloWorldVolatile2(glob);
143 }
144 catch (Throwable e) {
145 e.printStackTrace();
146 System.err.println("Unexpected problem: " + e.getMessage());
147 }
148 }
149 }
syntax highlighted by Code2HTML, v. 0.9.1