4 Replies Latest reply on Oct 22, 2013 10:52 AM by mschwery

    lookup remote ejb using java:global/as7project/SampleBeanRemoteImpl

    mschwery

      I'm trying to get a client in one project to look up a ejb in another project that JBoss says is java:global/as7project/SampleBeanRemoteImpl when it srarts up.

      Both projects are running on the same server. I have used the folling code and the ejb has @Remote.

       

      Properties props = new Properties();
      props.put(Context.PROVIDER_URL, "remote://localhost:4447");
      props.put(Context.SECURITY_PRINCIPAL, "ejbuser");
      props.put(Context.SECURITY_CREDENTIALS, "$ejbuser123");
      props.put("jboss.naming.client.ejb.context", true);
      props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
      InitialContext context = new InitialContext(props);
      System.out.println("\n\tGot initial Context: "+context);
      Object obj = context.lookup("java:global/as7project/SampleBeanRemoteImpl!com.sample.ejb.SampleBeanRemote");
      System.out.println("pr" + obj);
                     
      SampleBeanRemote remote = (SampleBeanRemote)javax.rmi.PortableRemoteObject.narrow(obj, SampleBeanRemote.class);
      System.out.println("remote" + remote);

       

      I am getting an class cast exception when I invoke the narrow command.

        • 1. Re: lookup remote ejb using java:global/as7project/SampleBeanRemoteImpl
          wdfink

          What kind of client do you use?

          If you run a remote standalone application you should read Remote EJB invocations via JNDI - EJB client API or remote-naming project

          But this is not the recommended approach to invoke EJB's.

           

          Try this approach EJB invocations from a remote client using JNDI

          • 2. Re: lookup remote ejb using java:global/as7project/SampleBeanRemoteImpl
            mschwery

            My lookup code in a war on server a is the following.

             

            try {
                          Logger.getLogger("org.jboss").setLevel(Level.OFF);
                          Logger.getLogger("org.xnio").setLevel(Level.OFF);
                          
                         
                          final  Hashtable jndiProps = new Hashtable();
                          jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
                          jndiProps.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED","false"); 
                          jndiProps.put("remote.connection.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS","false");
                          jndiProps.put("java.naming.factory.url.pkgs","org.jboss.ejb.client.naming");
                          jndiProps.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
                          jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4547");
                          Context ctx = new InitialContext(jndiProps);

             

                         Object obj = ctx.lookup("as7project/SampleBeanRemoteImpl!com.sample.ejb.SampleBeanRemote");
                         SampleBeanRemoteImpl service = (SampleBeanRemoteImpl)obj;
                         service.echo("This is a test");

                         ctx.close();


                      } catch (Throwable t) {
                          t.printStackTrace();
                      }

             

             

            The EJB is the following.

             

            package com.sample.ejb;

             

            import javax.ejb.Remote;
            import javax.ejb.Stateless;


            @Stateless
            @Remote(SampleBeanRemote.class)
            public class  SampleBeanRemoteImpl implements SampleBeanRemote  {

             

                @Override
                public String echo(String s) {

             

                    return "Hello "+s;
                }
            }

             

            The server says at startup.

            java:global/as7project/SampleBeanRemoteImpl!com.sample.ejb.SampleBeanRemote
            java:app/as7project/SampleBeanRemoteImpl!com.sample.ejb.SampleBeanRemote

            java:module/SampleBeanRemoteImpl!com.sample.ejb.SampleBeanRemote
            java:jboss/exported/as7project/SampleBeanRemoteImpl!com.sample.ejb.SampleBeanRemote
            java:global/as7project/SampleBeanRemoteImpl
            java:app/as7project/SampleBeanRemoteImpl
            java:module/SampleBeanRemoteImpl

             

            The lookup code is in a war on one server and the ejb is in a war on another server.

            I want the client war to go to the other server to get the ejb, there is no ejb on the server with the look code.

            We are not using security on the ejb.

            • 3. Re: lookup remote ejb using java:global/as7project/SampleBeanRemoteImpl
              wdfink

              As you use EJB3 there is no need to use narrow().

              Also you can not cast to the implementation, the client should not have the *Impl class in the classpath, you should use this instead:

                Object obj = ctx.lookup("as7project/SampleBeanRemoteImpl!com.sample.ejb.SampleBeanRemote");

                SampleBeanRemote service = (SampleBeanRemote)obj;

                service.echo("This is a test");

              • 4. Re: lookup remote ejb using java:global/as7project/SampleBeanRemoteImpl
                mschwery

                It ended up being that when I started the client and remote server I didn't assign node names.

                It works now.