4 Replies Latest reply on Oct 7, 2003 8:12 AM by Jon Barnett

    Two Server connection problem

    vincentchun Newbie

      I have two JBoss Server on two PC connected with LAN. Two server have different EJBs (of course, different functions).

      How can I call EJBs from one server to another server?

      client -> JBoss Server A -> JBoss Server B
      (do sth then call (do sth then return
      EJBs in Server B to Server A)
      return the result
      of Server A & B)

      For examples, UserHome is the Home of EJB in Server B. The following code is in the Server A call Server B EJB.

      try {
      InitialContext ctx = new InitialContext();
      Object objref = ctx.lookup("ejb/fyp/main/UserBean");
      UserHome home = (UserHome)PortableRemoteObject.narrow(objref, UserHome.class);
      } catch (Exception NamingException) {

      As there hasn't UserHome class in server A, how can it connection to server B?

      Is copy all the EJB class from Server A to Server B the only solution?

        • 1. Re: Two Server connection problem
          Jon Barnett Master

          In order to connect explicitly to the other JBoss system, you must include in the properties for your initial context, the URL for the remote server.


          Properties jndiProps = new Properties();
          jndiProps.setProperty("java.naming.provider.url", "jnp://localhost:1099");
          InitialContext jndiContext = new InitialContext(jndiProps);

          For a JBoss to JBoss call, you probably do not need to set up the JNDI packages and JNDI Factory, but you may want to do that for completeness. You must set up as though you are calling the other JBoss server as a remote client.

          Otherwise, you will need to place all the EJBs in the one JBoss server.

          • 2. Re: Two Server connection problem
            vincentchun Newbie

            Thanks for you help first!

            I have setting as follow:
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            env.put(Context.PROVIDER_URL, ServerBUrl);
            env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            try {
            InitialContext ctx = new InitialContext(env);
            Object objref = ctx.lookup(JNDIString);

            You say "You must set up as though you are calling the other JBoss server as a remote client."

            How can I set up as though the calling the other JBoss server as a remote client?

            Could you give me an example or part of code to doing that?

            • 3. Re: Two Server connection problem
              Sophie Wilson Newbie

              I'm also trying to make calls between EJBs in two instances of JBoss and am also encountering some difficulities. Any help you could give on this would be greatly appreciated.

              I thought it was possible to reference BeanB, deployed on the second server, through entries in ejb-jar.xml and jboss.xml for BeanA, deployed on the first.

              <..... remaining entries for BeanA ..../>


              And that by doing so it would use the JNDI service of the first server without explicitly having to initialise a new InitialContext with the jndi properties of the remote server.

              I haven't got this working yet but am I right in principle?

              • 4. Re: Two Server connection problem
                Jon Barnett Master

                In theory it should work. I've seen this configured a few times in this manner. As the test, I would suggest first checking by trying a context lookup directly with the jndi-name as a string in code.
                e.g. ctx.lookup("jnp://otherhost:1099/application/ejb/BeanB");

                If that works, try using the ejb-ref-name indirection.