1 // xmlBlaster/demo/javaclients/HelloWorldNative.java
2 package javaclients;
3 import org.xmlBlaster.client.qos.ConnectQos;
4 import org.xmlBlaster.client.I_XmlBlasterAccess;
5 import org.xmlBlaster.client.XmlBlasterAccess;
6 import org.xmlBlaster.util.Global;
7 import org.xmlBlaster.util.MsgUnit;
8 import org.xmlBlaster.util.XmlBlasterException;
9 import org.xmlBlaster.util.def.Constants;
10 import org.xmlBlaster.util.plugin.I_Plugin;
11 import org.xmlBlaster.util.plugin.PluginInfo;
12
13
14 /**
15 * This native client plugin is loaded by xmlBlaster on startup,
16 * it then connects to xmlBlaster.
17 * <p />
18 * You need to add this plugin to xmlBlasterPlugins.xml, for example:
19 * <pre>
20 * <plugin id='HelloWorldNative' className='javaclients.HelloWorldNative'>
21 * <action do='LOAD' onStartupRunlevel='3' sequence='0' onFail='resource.configuration.pluginFailed'/>
22 * <action do='STOP' onShutdownRunlevel='6' sequence='4'/>
23 * </plugin>
24 * </pre>
25 * As a protocol driver to talk to xmlBlaster it has configured "LOCAL", this
26 * plugin works only if client and server is in the same virtual machine (JVM).
27 * Other protocols like CORBA or SOCKET would work as well but carry the overhead
28 * of sending the message over TCP/IP.
29 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.local.html" target="others">native protocol requirement</a>
30 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.runlevel.html" target="others">run level requirement</a>
31 */
32 public class HelloWorldNative implements I_Plugin
33 {
34 private Global glob;
35
36 private final void doLogin() {
37 try {
38 System.err.println("HelloWorldNative: Connecting with protocol 'LOCAL' to xmlBlaster\n");
39 I_XmlBlasterAccess con = new XmlBlasterAccess(glob);
40
41 ConnectQos qos = new ConnectQos(this.glob); /* Client side object */
42 qos.setPtpAllowed(false);
43 qos.setUserId("A-NATIVE-CLIENT-PLUGIN");
44 qos.getSessionQos().setSessionTimeout(0L);
45 con.connect(qos, null); // Login to xmlBlaster as "A-NATIVE-CLIENT-PLUGIN"
46 //Here we can publish or subscribe etc., see HelloWorld3.java how to do it
47 con.publish(new MsgUnit(glob, "<key oid='TEST'/>", "Hi", "<qos/>"));
48 //con.disconnect(null);
49 }
50 catch (Exception e) {
51 System.err.println("HelloWorldNative: We have a problem: " + e.toString());
52 }
53 }
54
55 public void init(org.xmlBlaster.util.Global glob, PluginInfo pluginInfo) throws XmlBlasterException {
56 this.glob = glob.getClone(glob.getNativeConnectArgs()); // Sets "-protocol LOCAL" etc.
57 this.glob.addObjectEntry(Constants.OBJECT_ENTRY_ServerScope, glob.getObjectEntry(Constants.OBJECT_ENTRY_ServerScope));
58 System.out.println("\nHelloWorldNative: init(): The plugin is loaded");
59 doLogin();
60 }
61
62 public String getType() {
63 return "HelloWorldNative";
64 }
65
66 public String getVersion() {
67 return "1.0";
68 }
69
70 public void shutdown() throws XmlBlasterException {
71 System.err.println("\nHelloWorldNative: shutdown()\n");
72 }
73
74 /** To start as a plugin */
75 public HelloWorldNative() {}
76
77 /** To start as a separate client: java javaclients.HelloWorldNative */
78 public HelloWorldNative(String args[]) {
79 this.glob = new Global(args);
80 doLogin();
81 }
82
83 public static void main(String args[]) {
84 new HelloWorldNative(args);
85 }
86 }
syntax highlighted by Code2HTML, v. 0.9.1