1 Reply Latest reply on Mar 31, 2006 12:09 AM by Boris Zozoulia

    Same JNDI name on two different hosts problem

    Boris Zozoulia Newbie

      Hey there,

      I have two session beans deployed in a separate jars on separate jboss instances running on two different hosts.
      These session beans have the same jndi name.
      Both beans have method defined testMethod() which returns name of the host, this bean is deployed at.
      Both beans also have a runTest() method which executes testMethod() from both session beans (using remote interfaces) from different hosts.
      I am invoking runTest() method on one of the beans, which results in two calls to testMethod() method from the same bean!

      Problem is, that since both sessions have the same JNDI name, and even though they are located on different hosts, testMethod() gets invoked from the same host where runTest() method is running.

      How do I overcome this problem ?

      Im running jboss in the default configuration and it is crucial to have those beans under the same JNDI name.

      Sample code is below

      Session bean methods

       public String testMethod() throws TestComponentException
       {
       System.out.print("Entered testMethod()");
       String hostname;
       try
       {
       hostname = InetAddress.getLocalHost().getHostName();
       }
       catch (UnknownHostException he)
       {
       hostname = "localhost";
       }
       return hostname;
       }
      
       public String runTest() throws TestComponentException
       {
       String result = "";
       try
       {
       Properties props = new Properties();
      
       props.remove(Context.PROVIDER_URL);
       props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      
       props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      
       InitialContext ctx = new InitialContext(props);
      
       Object object = ctx.lookup("jnp://192.168.0.110/TestComponent");
      
       TestComponentHome eventManagerHome = (TestComponentHome) PortableRemoteObject.narrow(object, TestComponentHome.class);
      
       TestComponent eventManager = eventManagerHome.create();
      
       result +=("p: " + eventManager.testMethod());
      
       Properties props2 = new Properties();
       props2.remove(Context.PROVIDER_URL);
      
       props2.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      
       props2.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      
       InitialContext ctx2 = new InitialContext(props2);
      
       Object object2 = ctx2.lookup("jnp://192.168.0.120/TestComponent");
      
       TestComponentHome eventManagerHome2 = (TestComponentHome) PortableRemoteObject.narrow(object2, TestComponentHome.class);
      
       TestComponent eventManager2 = eventManagerHome2.create();
      
       result +=(" m: " + eventManager2.testMethod());
      
      
       System.out.print(result);
      
       }
       catch (Exception e)
       {
       e.printStackTrace();
       throw new TestComponentException(e);
       }
       return result;
       }
      


      Client call

      Properties props = new Properties();
      
       props.remove(Context.PROVIDER_URL);
       //props.put(Context.PROVIDER_URL, "sa-provisioning");
      
       props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      
       props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      
       InitialContext ctx = new InitialContext(props);
      
       Object object = ctx.lookup("jnp://192.168.0.110/TestComponent");
      
       TestComponentHome eventManagerHome = (TestComponentHome) PortableRemoteObject.narrow(object, TestComponentHome.class);
       //TestComponentHome eventManagerHome = (TestComponentHome) object;
      
       TestComponent eventManager = eventManagerHome.create();
      
       System.out.println("calling runTest() from provisioning results: " + eventManager.runTest());


      This code result in two System.out.printlines - "Entered testMethod()" on the 192.168.0.110 hosts, as opposed to one line on both hosts.