1 package org.xmlBlaster.test.memoryleak;
  2 
  3 import java.util.logging.Logger;
  4 import java.util.logging.Level;
  5 import org.xmlBlaster.util.Global;
  6 import org.xmlBlaster.util.XmlBlasterException;
  7 import org.xmlBlaster.util.MsgUnit;
  8 import org.xmlBlaster.client.I_Callback;
  9 import org.xmlBlaster.client.key.UpdateKey;
 10 import org.xmlBlaster.client.qos.ConnectQos;
 11 import org.xmlBlaster.client.qos.ConnectReturnQos;
 12 import org.xmlBlaster.client.qos.DisconnectQos;
 13 import org.xmlBlaster.client.qos.PublishQos;
 14 import org.xmlBlaster.client.qos.UpdateQos;
 15 import org.xmlBlaster.client.I_XmlBlasterAccess;
 16 
 17 import java.io.*;
 18 
 19 
 20 /**
 21  * You can use this client to test with tools like OptimizeIt if we
 22  * have memory leaks with volatile messages.
 23  *
 24  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/src/java/org/xmlBlaster/protocol/corba/xmlBlaster.idl" target="others">CORBA xmlBlaster.idl</a>
 25  */
 26 public class Volatile
 27 {
 28    private final String ME = "Volatile";
 29    private static Logger log = Logger.getLogger(Volatile.class.getName());
 30    private I_XmlBlasterAccess con = null;
 31    private ConnectReturnQos conRetQos = null;
 32    private boolean connected;
 33    private int bulkSize = 10;
 34 
 35    public Volatile(final Global glob) {
 36       
 37 
 38       bulkSize = glob.getProperty().get("bulkSize", bulkSize);
 39       long lCount = 0L;
 40 
 41       try {
 42          con = glob.getXmlBlasterAccess();
 43          ConnectQos qos = new ConnectQos(glob);
 44          conRetQos = con.connect(qos, new I_Callback() {
 45 
 46             public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
 47                log.info("Receiving asynchronous message '" + updateKey.getOid() +
 48                                "' state=" + updateQos.getState() + " in default handler");
 49                return "";
 50             }
 51 
 52          });  // Login to xmlBlaster, default handler for updates
 53 
 54          String xmlKey = null;
 55          PublishQos qw = new PublishQos(glob);
 56          qw.setVolatile(true);
 57          System.out.println("qos = " + qw.toXml() );
 58          byte[] b = new byte[1024];
 59          while(true) {
 60             lCount++;
 61             xmlKey =  "<key oid='" + lCount +
 62                            "'> <topic id='aaaa'/>" +
 63                            "</key>";
 64             con.publish(new MsgUnit(xmlKey,b,qw.toXml()));
 65             // System.out.println(new Timestamp(System.currentTimeMillis())+":"+lCount);
 66             if ((lCount % bulkSize) == 0) {
 67                log.info("Sent " + lCount + " different topics, enter return to continue, enter 'q' to quit");
 68                try {
 69                   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 70                   String line = in.readLine(); // Blocking in I/O
 71                   if (line == null) continue;
 72                   line = line.trim();
 73                   if (line.toLowerCase().equals("q")) {
 74                      break;
 75                   }
 76                }
 77                catch(Exception e) {
 78                   log.severe(e.toString());
 79                   break;
 80                }
 81             }
 82 
 83             try { Thread.sleep(5L); } catch( InterruptedException i) {}
 84          }
 85       }
 86       catch (XmlBlasterException e) {
 87          log.severe("Houston, we have a problem count=" + lCount + ": " + e.toString());
 88       }
 89       finally {
 90          log.info("Success, hit a key to logout and exit");
 91          try { System.in.read(); } catch(java.io.IOException e) {}
 92          con.disconnect(new DisconnectQos(glob));
 93       }
 94    }
 95 
 96    /**
 97     * Try
 98     * <pre>
 99     *   java org.xmlBlaster.test.memoryleak.Volatile -help
100     * </pre>
101     * for usage help
102     */
103    public static void main(String args[]) {
104       Global glob = new Global();
105       
106       if (glob.init(args) != 0) { // Get help with -help
107          System.out.println(glob.usage());
108          System.out.println("Example: java Volatile -bulkSize 10\n");
109          System.exit(1);
110       }
111 
112       new Volatile(glob);
113    }
114 }


syntax highlighted by Code2HTML, v. 0.9.1