8 Replies Latest reply on Jul 17, 2003 3:10 AM by frito

    How to lookup EJB from external app

    sbahal

      I have a EJB successfully deployed in JBOSS. I can deploy lookup the Local and Remote from within JBoss- but I need to lookup from an external app. It simply does not work- I get funky errors. If I try it this way:
      String bindName ="AccountServiceEJB";
      String host ="loclhost";
      String port ="1099";
      String serverName= "rmi://" + host + ":" + port;
      Properties cosNamingProperties = new Properties();
      cosNamingProperties.put("java.naming.factory.initial",
      "com.sun.jndi.cosnaming.CNCtxFactory");
      cosNamingProperties.put("java.naming.provider.url", serverName);
      InitialContext ctx = new InitialContext(cosNamingProperties);
      ctx = new InitialContext(cosNamingProperties);
      System.out.println("RMITestServlet Context Created");
      Object obj = ctx.lookup(bindName);

      This gives error unknown protocol rmi.
      If I try:
      cosNamingProperties.put(
      InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      cosNamingProperties.put(InitialContext.PROVIDER_URL, "jnp://"+host+":"+port);
      This first gives error can't find jnp I add that jar file into class path then it gives error:
      java.lang.NoClassDefFoundError: org/jboss/logging/Logger
      I don't know where to find this file.
      Is there a standard way to do this.
      Thanks a lot,
      Sanjay

        • 1. Re: How to lookup EJB from external app
          sbahal

          I could get rid of this error by including the jboss-common.jar - that is such a pain- my clients may not have these files. But now I get this new error:
          Exception:javax.naming.CommunicationException [Root exception is java.lang.ClassNotFoundException: org.jboss.proxy.ClientContainer (no security manager: RMI class loader disabled)]

          I have tried it two ways:
          1. cosNamingProperties.put( InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
          cosNamingProperties.put(InitialContext.PROVIDER_URL, "jnp://"+host+":"+port);
          cosNamingProperties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
          2. cosNamingProperties.put( "java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory" );
          cosNamingProperties.put( "java.naming.provider.url", host+":"+port ) ;
          cosNamingProperties.put( "java.naming.factory.url.pkgs", "org.jboss.naming" ) ;

          I get the same error in both cases.
          Thanks again,
          Sanjay

          • 2. Re: How to lookup EJB from external app- It works
            sbahal

            It finally works:
            To get rid of the RMI error you need to setup the regular RMI security with policy file etc:
            cosNamingProperties.put( "java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory" );
            cosNamingProperties.put( "java.naming.provider.url", host+":"+port ) ;

            So to sum up you need the flwg files in the client path:
            jnpserver.jar, jboss-common.jar
            The code is:
            Properties cosNamingProperties = new Properties();
            if (System.getSecurityManager() == null) {
            System.setProperty("java.security.policy","server.policy"); //the policy file
            System.setSecurityManager(new RMISecurityManager());
            }
            cosNamingProperties.put( "java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory" );
            cosNamingProperties.put( "java.naming.provider.url", host+":"+port ) ;
            InitialContext ctx = new InitialContext(cosNamingProperties);
            ctx = new InitialContext(cosNamingProperties);
            Object obj = ctx.lookup(bindName);


            If anybody knows how to (if at all possible) to do the lookup without the jboss specific jar files.

            Thanks a lot,
            Sanjay

            • 3. Re: How to lookup EJB from external app- It works
              imdkidd

              If you don't want to hard code the JNDI parameters, you could always include a jndi.properties file with the url and context factory and include it in your WEB-INF/classes directory.

              • 4. Re: How to lookup EJB from external app- It works
                frito

                You don't ever need to include server .jar files with your client. All libs you need are in your /client directory. Try with jbossall-client.jar which includes everything a client could need or determine which jars you need if you don't want to include the whole client thing at all.

                Greetings,
                Frito

                • 5. Re: How to lookup EJB from external app- It works
                  sbahal

                  Frito- Yes I can use the jbossall-client.jar that does reduce the jar files from 2 to 1.
                  Ideally I would like to use some generic jdk files to do lookup- if that is at all possible.

                  imdkidd- Yes I can use the property file for the setup information. My issue is the jar files and doing JBoss specific lookup things.
                  Thanks
                  Sanjay

                  • 6. Re: How to lookup EJB from external app- It works
                    frito

                    This is not possible, since you receive an implementation for a stub. This stub is communicating with the skeleton on the server. These implementations are vendor specific. Therefore you need a client.jar for every appserver you want to use.

                    Greetings,
                    Frito

                    • 7. Re: How to lookup EJB from external app- It works
                      sbahal

                      Frito,
                      Thanks. I undersatnd what you are saying. But this just breaks the whole concept of a client being independent/agnostic of the server. In this case it seems it is very dependent.
                      Sanjay

                      • 8. Re: How to lookup EJB from external app- It works
                        frito

                        Remember: the client code is not dependent on what application server you are working. The implementations are behind standard interfaces at all. But the running instance is, of course. You need the resources to access the server implementation your are working with.

                        Greetings,
                        Frito