1 // xmlBlaster/demo/javaclients/LeaveServer.java
  2 package javaclients;
  3 
  4 import java.io.UnsupportedEncodingException;
  5 import java.util.Iterator;
  6 import java.util.Map;
  7 import java.util.logging.Logger;
  8 
  9 import org.xmlBlaster.client.I_Callback;
 10 import org.xmlBlaster.client.I_ConnectionStateListener;
 11 import org.xmlBlaster.client.I_XmlBlasterAccess;
 12 import org.xmlBlaster.client.XmlBlasterAccess;
 13 import org.xmlBlaster.client.key.UpdateKey;
 14 import org.xmlBlaster.client.qos.ConnectQos;
 15 import org.xmlBlaster.client.qos.DisconnectQos;
 16 import org.xmlBlaster.client.qos.UpdateQos;
 17 import org.xmlBlaster.util.Global;
 18 import org.xmlBlaster.util.XmlBlasterException;
 19 import org.xmlBlaster.util.dispatch.ConnectionStateEnum;
 20 
 21 /**
 22  * Test to leave the server without destroying the server side session. 
 23  * <p>
 24  * Invoke (after starting the xmlBlaster server):
 25  * </p>
 26  * <pre>
 27  * Test leaving 10 times
 28  * java javaclients.LeaveServer -interactive true -count 10
 29  * </pre>
 30  * 
 31  * @see <a
 32  *      href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html"
 33  *      target="others">xmlBlaster interface</a>
 34  */
 35 public class LeaveServer {
 36    private static Logger log = Logger.getLogger(LeaveServer.class.getName());
 37 
 38    public LeaveServer(Global glob) {
 39 
 40       try {
 41          int countRuns = glob.getProperty().get("count", 10);
 42          boolean interactive = glob.getProperty().get("interactive", true);
 43          boolean connectPersistent = glob.getProperty().get(
 44                "connect/qos/persistent", false);
 45          Map connectQosClientPropertyMap = glob.getProperty().get(
 46                "connect/qos/clientProperty", (Map) null);
 47 
 48          log.info("Used settings are:");
 49          log.info("   -interactive    " + interactive);
 50          log.info(" ConnectQos settings");
 51          log.info("   -connect/qos/persistent " + connectPersistent);
 52          if (connectQosClientPropertyMap != null) {
 53             Iterator it = connectQosClientPropertyMap.keySet().iterator();
 54             while (it.hasNext()) {
 55                String key = (String) it.next();
 56                log.info("   -connect/qos/clientProperty[" + key + "]   "
 57                      + connectQosClientPropertyMap.get(key).toString());
 58             }
 59          } else {
 60             log.info("   -connect/qos/clientProperty[]   ");
 61          }
 62          log.info("For more info please read:");
 63          log
 64                .info("   http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.connect.html");
 65 
 66          for (int count = 0; count < countRuns; count++) {
 67 
 68             char ret = 0;
 69             if (interactive) {
 70                //while (ret != 'l' && ret != 'd' && ret != 'q')
 71                   ret = (char) Global
 72                         .waitOnKeyboardHit("Hit 'l' to connect and leave server (default), 'd' to connect and disconnect, 'q' to quit");
 73             }
 74             
 75             if (ret == 'q')
 76                break;
 77 
 78             I_XmlBlasterAccess con = new XmlBlasterAccess(glob.getClone(null));
 79 
 80             con.registerConnectionListener(new I_ConnectionStateListener() {
 81 
 82                public void reachedAlive(ConnectionStateEnum oldState,
 83                      I_XmlBlasterAccess connection) {
 84                   log.info("I_ConnectionStateListener: Connected");
 85                }
 86 
 87                public void reachedPolling(ConnectionStateEnum oldState,
 88                      I_XmlBlasterAccess connection) {
 89                   log
 90                         .warning("I_ConnectionStateListener: No connection to xmlBlaster server, we are polling ...");
 91                }
 92 
 93                public void reachedDead(ConnectionStateEnum oldState,
 94                      I_XmlBlasterAccess connection) {
 95                   log.warning("I_ConnectionStateListener: Connection from "
 96                         + connection.getGlobal().getId()
 97                         + " to xmlBlaster is DEAD, doing exit.");
 98                }
 99                public void reachedAliveSync(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
100                }
101 
102             });
103 
104             ConnectQos qos = new ConnectQos(glob);
105             if (connectPersistent) {
106                qos.setPersistent(connectPersistent);
107             }
108             if (connectQosClientPropertyMap != null) {
109                Iterator it = connectQosClientPropertyMap.keySet().iterator();
110                while (it.hasNext()) {
111                   String key = (String) it.next();
112                   qos.addClientProperty(key, connectQosClientPropertyMap.get(
113                         key).toString());
114                }
115             }
116             //log.info("ConnectQos is " + qos.toXml());
117             /* ConnectReturnQos crq = */con.connect(qos, new I_Callback() {
118                public String update(String cbSessionId, UpdateKey updateKey,
119                      byte[] content, UpdateQos updateQos)
120                      throws XmlBlasterException {
121                   try {
122                      log.info("Received '" + updateKey.getOid() + "':"
123                            + new String(content, "UTF-8"));
124                   } catch (UnsupportedEncodingException e) {
125                      log.severe("Update failed: " + e.toString());
126                   }
127                   return "";
128                }
129             }); // Login to xmlBlaster, register for updates
130             log.info("Connect success");
131 
132             if (ret == 'd') {
133                DisconnectQos dq = new DisconnectQos(glob);
134                con.disconnect(dq);
135                log.info("Disconnected from server, all resources released");
136             } else {
137                con.leaveServer(null);
138                ret = 0;
139                log.info("Left server, our server side session remains, bye");
140             }
141             con = null;
142 
143             Global.gc(2, 10L);
144             log.info("Count=" + count + ": " + Global.getMemoryStatistic());
145          }
146          log.info("Bye");
147       } catch (XmlBlasterException e) {
148          log.severe(e.getMessage());
149       } catch (Exception e) {
150          e.printStackTrace();
151          log.severe(e.toString());
152       }
153    }
154 
155    /**
156     * Try
157     * 
158     * <pre>
159     *   java javaclients.LeaveServer -help
160     * </pre>
161     * 
162     * for usage help
163     */
164    public static void main(String args[]) {
165       Global glob = new Global();
166 
167       if (glob.init(args) != 0) { // Get help with -help
168          System.out.println(glob.usage());
169          System.err.println("\nExample:");
170          System.err
171                .println("  java javaclients.LeaveServer -interactive false\n");
172          System.exit(1);
173       }
174 
175       new LeaveServer(glob);
176    }
177 }


syntax highlighted by Code2HTML, v. 0.9.1