1 // xmlBlaster/demo/javaclients/HelloWorldNative2.java
  2 package javaclients;
  3 import java.util.logging.Logger;
  4 import java.util.logging.Level;
  5 import org.xmlBlaster.client.qos.ConnectQos;
  6 import org.xmlBlaster.client.I_XmlBlasterAccess;
  7 import org.xmlBlaster.client.XmlBlasterAccess;
  8 import org.xmlBlaster.client.I_Callback;
  9 import org.xmlBlaster.client.key.PublishKey;
 10 import org.xmlBlaster.client.qos.PublishQos;
 11 import org.xmlBlaster.client.qos.PublishReturnQos;
 12 import org.xmlBlaster.client.key.SubscribeKey;
 13 import org.xmlBlaster.client.qos.SubscribeQos;
 14 import org.xmlBlaster.client.qos.SubscribeReturnQos;
 15 import org.xmlBlaster.client.key.UpdateKey;
 16 import org.xmlBlaster.client.qos.UpdateQos;
 17 import org.xmlBlaster.util.Global;
 18 import org.xmlBlaster.util.XmlBlasterException;
 19 import org.xmlBlaster.util.MsgUnit;
 20 import org.xmlBlaster.util.def.Constants;
 21 import org.xmlBlaster.util.plugin.I_Plugin;
 22 import org.xmlBlaster.util.plugin.PluginInfo;
 23 
 24 
 25 /**
 26  * This native client plugin is loaded by xmlBlaster on startup,
 27  * it then connects to xmlBlaster and subscribes to a topic and publishes a message.
 28  * <p />
 29  * You need to register this plugin to xmlBlasterPlugins.xml, for example:
 30  * <pre>
 31  *  &lt;plugin id='HelloWorldNative2' className='javaclients.HelloWorldNative2'>
 32  *     &lt;attribute id='loginName'>nativeClient2&lt;/attribute>
 33  *     &lt;attribute id='topicName'>aNativeTopic2&lt;/attribute>
 34  *     &lt;action do='LOAD' onStartupRunlevel='9' sequence='6' onFail='resource.configuration.pluginFailed'/>
 35  *     &lt;action do='STOP' onShutdownRunlevel='6' sequence='5'/>
 36  *  &lt;/plugin>
 37  * </pre>
 38  * <p>
 39  * Note how the attributes <i>loginName</i> and <i>topicName</i> are passed to the plugin.
 40  * </p>
 41  * <p>
 42  * As a protocol driver to talk to xmlBlaster it has configured "LOCAL", this
 43  * plugin works only if client and server is in the same virtual machine (JVM).
 44  * Other protocols like CORBA or SOCKET would work as well but carry the overhead
 45  * of sending the message over TCP/IP.
 46  * </p>
 47  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.runlevel.html" target="others">run level requirement</a>
 48  */
 49 public class HelloWorldNative2 implements I_Plugin
 50 {
 51    private Global glob;
 52    private PluginInfo pluginInfo;
 53    private static Logger log = Logger.getLogger(HelloWorldNative2.class.getName());
 54    private String loginName;
 55    private String topicName;
 56    private I_XmlBlasterAccess con;
 57 
 58    private final void pubsub() {
 59       try {
 60          log.info("Connecting with protocol 'LOCAL' to xmlBlaster");
 61          con = new XmlBlasterAccess(glob);
 62 
 63          ConnectQos qos = new ConnectQos(this.glob); /* Client side object */
 64          qos.setUserId(this.loginName);
 65          qos.getSessionQos().setSessionTimeout(0L);
 66          con.connect(qos, new I_Callback() {
 67 
 68             public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 69                if (log.isLoggable(Level.FINEST)) log.finest("UpdateKey.toString()=" + updateKey.toString() +
 70                                           "UpdateQos.toString()=" + updateQos.toString());
 71                if (updateKey.isInternal()) {
 72                   log.severe("Receiving unexpected asynchronous internal message '" + updateKey.getOid() +
 73                                 "' in default handler");
 74                   return "";
 75                }
 76                if (updateQos.isErased()) {
 77                   log.info("Message '" + updateKey.getOid() + "' is erased");
 78                   return "";
 79                }
 80                if (updateKey.getOid().equals(topicName))
 81                   log.info("Receiving asynchronous message '" + updateKey.getOid() +
 82                                "' state=" + updateQos.getState() + " in default handler");
 83                else
 84                   log.severe("Receiving unexpected asynchronous message '" + updateKey.getOid() +
 85                                    "' in default handler");
 86                return "";
 87             }
 88 
 89          });
 90 
 91          SubscribeKey sk = new SubscribeKey(glob, this.topicName);
 92          SubscribeQos sq = new SubscribeQos(glob);
 93          sq.setWantInitialUpdate(false);
 94          SubscribeReturnQos sr1 = con.subscribe(sk, sq);
 95          log.info("Subscribed '" + sr1.getState() + "'");
 96 
 97          PublishKey pk = new PublishKey(glob, this.topicName, "text/plain", "1.0");
 98          PublishQos pq = new PublishQos(glob);
 99          MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
100          PublishReturnQos retQos = con.publish(msgUnit);
101          log.info("Published message '" + retQos.getKeyOid() + "'");
102 
103       }
104       catch (Exception e) {
105          log.severe("We have a problem: " + e.toString());
106       }
107    }
108 
109    public void init(org.xmlBlaster.util.Global glob, PluginInfo pluginInfo) throws XmlBlasterException {
110       this.pluginInfo = pluginInfo;
111       this.glob = glob.getClone(glob.getNativeConnectArgs()); // Sets  "-protocol LOCAL" etc.
112 
113       // "ServerNodeScope"
114       this.glob.addObjectEntry(Constants.OBJECT_ENTRY_ServerScope, glob.getObjectEntry(Constants.OBJECT_ENTRY_ServerScope));
115 
116       this.loginName = glob.get("loginName", "NO_LOGIN_NAME_CONFIGURED", null, pluginInfo);
117       this.topicName = glob.get("topicName", "NO_TOPIC_NAME_CONFIGURED", null, pluginInfo);
118 
119       log.info("init(): The plugin is loaded, doing a publish and subscribe\n\n");
120       pubsub();
121    }
122 
123    public String getType() {
124       return (this.pluginInfo==null)?"HelloWorldNative2":this.pluginInfo.getType();
125    }
126 
127    public String getVersion() {
128       return (this.pluginInfo==null)?"1.0":this.pluginInfo.getVersion();
129    }
130 
131    public void shutdown() {
132       // cleans up our subscribe as well
133       if (con != null) con.disconnect(null);
134       log.info("shutdown()\n");
135    }
136 
137    public void finalize() {
138       log.info("Garbage collected");
139    }
140 
141    /** To start as a plugin */
142    public HelloWorldNative2() {}
143 
144    /** To start as a standalone client: java javaclients.HelloWorldNative2 */
145    public HelloWorldNative2(String args[]) {
146       this.glob = new Global(args);
147 
148       this.loginName = "A-native-client";
149       this.topicName = "A-native-message";
150       pubsub();
151 
152       shutdown();
153    }
154 
155    public static void main(String args[]) {
156       new HelloWorldNative2(args);
157    }
158 }


syntax highlighted by Code2HTML, v. 0.9.1