[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [xmlblaster] bug in multiple (parallel) connections



Michael Atighetchi wrote:
A problem with multiple connection shows up in the following code,
which sets up IOR, SOCKET, XML-RPC, and RMI connections:

Hi Michael,

i believe the problem you experience is related
to using the same Global instance for each connection.

Please use the XmlBlasterConnection(Global glob)
constructor to pass a unique Global instance, e.g.

   ...
 Global glob = new Global(conargs, true, false);
 XmlBlasterConnection con = new XmlBlasterConnection(glob);
   ...

I have attached an example file,

regards,

Marcel

XmlBlasterConnection con_corba_; XmlBlasterConnection con_xmlrpc_; XmlBlasterConnection con_socket_; XmlBlasterConnection con_rmi_;


String[] conargs = new String[4]; String ip = prop.getProperty("ap1_ip");

conargs[0]="-client.protocol";
conargs[1]="IOR";
conargs[2]="-ior.file";
conargs[3]="djmproxy1.ior";
con_corba_ = new XmlBlasterConnection (conargs);
conargs = new String[6];
conargs[0]="-client.protocol";
conargs[1]="SOCKET";
conargs[2]="-socket.hostname";
conargs[3]=ip;
conargs[4]="-socket.port";
conargs[5]=prop.getProperty("ap1_socket");
con_socket_ = new XmlBlasterConnection (conargs);
conargs[1]="XML-RPC";
conargs[2]="-xmlrpc.hostname";
conargs[3]=ip;
conargs[4]="-xmlrpc.port";
conargs[5]=prop.getProperty("ap1_xmlrpc");
con_xmlrpc_ = new XmlBlasterConnection (conargs);


   conargs[1]="RMI";
   conargs[2]="-rmi.hostname";
   conargs[3]=ip;
   conargs[4]="-rmi.registryPort";
   conargs[5]=prop.getProperty("ap1_rmi");
   con_rmi_ = new XmlBlasterConnection (conargs);


XmlBlasterConnection con = con_xmlrpc_; con.connect (connectQos, null) con.publish (...)

The strage thing is that the log printout from the client indicates
that the RMI connection is being setup, and a trace with tethereal
confirmed that RMI traffic goes out of the client when it publishes
messages. Switching the order of the last two connection
initializations to

   conargs[1]="RMI";
   conargs[2]="-rmi.hostname";
   conargs[3]=ip;
   conargs[4]="-rmi.registryPort";
   conargs[5]=prop.getProperty("ap1_rmi");
   con_rmi_ = new XmlBlasterConnection (conargs);

   conargs[1]="XML-RPC";
   conargs[2]="-xmlrpc.hostname";
   conargs[3]=ip;
   conargs[4]="-xmlrpc.port";
   conargs[5]=prop.getProperty("ap1_xmlrpc");
   con_xmlrpc_ = new XmlBlasterConnection (conargs);


XmlBlasterConnection con = con_xmlrpc_; con.connect (connectQos, null) con.publish (...)

seems to have the effect that now the XML_RPC connection is being
used.

Overall, it seems that multiple connections cannot be used in
parallel, although this is what I need.

Comments ?






// xmlBlaster/demo/javaclients/AllProtocols.java
package javaclients;

import org.jutils.log.LogChannel;
import org.xmlBlaster.util.Global;
import org.xmlBlaster.util.MsgUnit;
import org.xmlBlaster.util.XmlBlasterException;
import org.xmlBlaster.client.qos.ConnectQos;
import org.xmlBlaster.client.qos.ConnectReturnQos;
import org.xmlBlaster.client.qos.DisconnectQos;
import org.xmlBlaster.client.I_Callback;
import org.xmlBlaster.client.key.UpdateKey;
import org.xmlBlaster.client.key.PublishKey;
import org.xmlBlaster.client.key.GetKey;
import org.xmlBlaster.client.key.SubscribeKey;
import org.xmlBlaster.client.key.UnSubscribeKey;
import org.xmlBlaster.client.key.EraseKey;
import org.xmlBlaster.client.qos.GetQos;
import org.xmlBlaster.client.qos.GetReturnQos;
import org.xmlBlaster.client.qos.PublishQos;
import org.xmlBlaster.client.qos.PublishReturnQos;
import org.xmlBlaster.client.qos.UpdateQos;
import org.xmlBlaster.client.qos.UpdateReturnQos;
import org.xmlBlaster.client.qos.SubscribeQos;
import org.xmlBlaster.client.qos.SubscribeReturnQos;
import org.xmlBlaster.client.qos.EraseQos;
import org.xmlBlaster.client.qos.EraseReturnQos;
import org.xmlBlaster.client.qos.UnSubscribeQos;
import org.xmlBlaster.client.qos.UnSubscribeReturnQos;
import org.xmlBlaster.client.protocol.XmlBlasterConnection;


/**
 * This client connects to xmlBlaster and invokes all available methods. 
 * <p />
 * We use java client helper classes to generate the raw xml strings, e.g.:
 * <pre>
 *   PublishKey pk = new PublishKey(glob, "AllProtocols", "text/xml");
 * 
 * generates:
 *
 *   &lt;key oid='AllProtocols' contentMime='text/xml'/>
 * </pre>
 *
 * Invoke: java javaclients.AllProtocols
 * <p />
 * Invoke: java javaclients.AllProtocols -session.name joe -passwd secret
 *  at see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html"; target="others">xmlBlaster interface</a>
 */
public class AllProtocols implements I_Callback
{
   private String ME = "";
   private final Global glob;
   private final LogChannel log;
   private final String[] argsIOR = {
      "-client.protocol",
      "IOR",
   };
   private final String[] argsSocket = {
      "-client.protocol",
      "SOCKET",
   };
   private final String[] argsXmlRpc = {
      "-client.protocol",
      "XML-RPC",
   };
   private final String[] argsRmi = {
      "-client.protocol",
      "RMI",
   };
   private final Con[] conList = {
      new Con(argsIOR, "IOR connection"),
      new Con(argsSocket, "SOCKET connection"),
      new Con(argsXmlRpc, "XML-RPC connection"),
      new Con(argsRmi, "RMI connection")
   };

   public AllProtocols(Global glob) {
      this.glob = glob;
      this.log = glob.getLog(null);
      for(int i=0; i<conList.length; i++) {
         ME = conList[i].helpText;
         try {
            conList[i].con = new XmlBlasterConnection(conList[i].glob);
            XmlBlasterConnection con = conList[i].con;

            // Check if other login name or password was given on command line:
            // (This is redundant as it is done by ConnectQos already)
            String name = glob.getProperty().get("session.name", "AllProtocols");
            String passwd = glob.getProperty().get("passwd", "secret");

            ConnectQos qos = new ConnectQos(glob, name, passwd);
            con.connect(qos, this);  // Login to xmlBlaster, register for updates


            PublishKey pk = new PublishKey(glob, "AllProtocols", "text/xml", "1.0");
            pk.setClientTags("<org.xmlBlaster><demo/></org.xmlBlaster>");
            PublishQos pq = new PublishQos(glob);
            MsgUnit msgUnit = new MsgUnit(glob, pk, "Hi", pq);
            con.publish(msgUnit);


            GetKey gk = new GetKey(glob, "AllProtocols");
            GetQos gq = new GetQos(glob);
            MsgUnit[] msgs = con.get(gk.toXml(), gq.toXml());
            GetReturnQos grq = new GetReturnQos(glob, msgs[0].getQos());

            log.info(ME, "Accessed xmlBlaster message with content '" + new String(msgs[0].getContent()) +
                         "' and status=" + grq.getState());


            SubscribeKey sk = new SubscribeKey(glob, "AllProtocols");
            SubscribeQos sq = new SubscribeQos(glob);
            SubscribeReturnQos subRet = con.subscribe(sk.toXml(), sq.toXml());


            msgUnit = new MsgUnit(glob, pk, "Ho".getBytes(), pq);
            PublishReturnQos prq = con.publish(msgUnit);

            log.info(ME, "Got status='" + prq.getState() + "' for published message '" + prq.getKeyOid());

            try { Thread.currentThread().sleep(1000); } 
            catch( InterruptedException ie) {} // wait a second to receive update()


            UnSubscribeKey uk = new UnSubscribeKey(glob, subRet.getSubscriptionId());
            UnSubscribeQos uq = new UnSubscribeQos(glob);
            UnSubscribeReturnQos[] urq = con.unSubscribe(uk.toXml(), uq.toXml());

            EraseKey ek = new EraseKey(glob, "AllProtocols");
            EraseQos eq = new EraseQos(glob);
            EraseReturnQos[] eraseArr = con.erase(ek.toXml(), eq.toXml());

            DisconnectQos dq = new DisconnectQos(glob);
            con.disconnect(dq);
         }
         catch (XmlBlasterException e) {
            log.error(ME, e.getMessage());
         }
         catch (Throwable e) {
            e.printStackTrace();
            log.error(ME, e.toString());
         }
      }
   }

   public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
                        UpdateQos updateQos)
   {
      if (updateKey.isInternal()) {
         log.info("", "Received unexpected internal message '" +
              updateKey.getOid() + " from xmlBlaster");
         return "";
      }

      log.info(ME, "Received asynchronous message '" + updateKey.getOid() +
                   "' state=" + updateQos.getState() +
                   " content=" + new String(content) + " from xmlBlaster");

      UpdateReturnQos uq = new UpdateReturnQos(glob);
      return uq.toXml();
   }

   /**
    * Try
    * <pre>
    *   java javaclients.AllProtocols -help
    * </pre>
    * for usage help
    */
   public static void main(String args[]) {
      Global glob = new Global();
      
      if (glob.init(args) != 0) { // Get help with -help
         XmlBlasterConnection.usage();
         System.err.println("Example: java javaclients.AllProtocols -session.name Jeff\n");
         System.exit(1);
      }

      new AllProtocols(glob);
   }
}

class Con {
   public Con(String[] args, String helpText) {
      this.glob = new Global(args, true, false);
      this.helpText = helpText;
   }
   public String helpText;
   public Global glob;
   public XmlBlasterConnection con;
};