3 Replies Latest reply on Sep 7, 2009 1:25 PM by peterj

    EJB 3.0 and JNDI problems

      Hello all!

      I need some help with the following:

      Web Server - Jboss 5.1.0 GA
      IDE - eclipse

      I created a EJB project using eclipse and the following files:

      ejb.server.HelloWorld.HelloWorldBeanRemote

      package ejb.server.HelloWorld;
      import javax.ejb.Remote;
      
      @Remote
      public interface HelloWorldBeanRemote {
       public void sayHelloRemoto();
      }
      
      
      


      ejb.server.HelloWorld.HelloWorldBeanLocal
      package ejb.server.HelloWorld;
      import javax.ejb.Local;
      
      @Local
      public interface HelloWorldBeanLocal {
       public void sayHelloLocal();
      }
      
      



      ejb.server.HelloWorld.HelloWorldBean
      package ejb.server.HelloWorld;
      
      import javax.ejb.Stateless;
      
      /**
       * Session Bean implementation class HelloWorldBean
       */
      @Stateless
      public class HelloWorldBean implements HelloWorldBeanRemote, HelloWorldBeanLocal {
      
       /**
       * Default constructor.
       */
       public HelloWorldBean() {
       // TODO Auto-generated constructor stub
       }
      
       @Override
       public void sayHelloRemoto() {
       // TODO Auto-generated method stub
       System.out.println("Remoto");
       }
      
       @Override
       public void sayHelloLocal() {
       // TODO Auto-generated method stub
       System.out.println("Local");
       }
      
      }
      
      



      and the client

      package ejb.client;
      
      import java.util.Properties;
      
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.rmi.PortableRemoteObject;
      
      import ejb.server.HelloWorld.HelloWorldBean;
      import ejb.server.HelloWorld.HelloWorldBeanRemote;
      
      public class EJBClient {
      
       /**
       * @param args
       */
       public static void main(String[] args) {
       // TODO Auto-generated method stub
       try {
       Properties properties = new Properties();
       properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
       properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
       properties.put("java.naming.provider.url","jnp://localhost:1099");
       InitialContext context = new InitialContext(properties);
      
      
       Object bean = context.lookup("HelloWorldBean/remote");
       Object bean2 = context.lookup(HelloWorldBean.class.getSimpleName()+"/remote");
      
       HelloWorldBeanRemote beanH = (HelloWorldBeanRemote)PortableRemoteObject.narrow(bean, HelloWorldBeanRemote.class);
       //Object bean3 = context.lookup("HelloWorldBean/local");
       //bean.sayHelloRemoto();
       System.out.println(bean);
       System.out.println(bean2);
       System.out.println(beanH);
      
       } catch (NamingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       }
      
      }
      
      
      


      I execute the project from Eclipse and my ejb is deployed as I get the message:
      11:27:41,999 INFO [EJBContainer] STARTED EJB: ejb.server.HelloWorld.HelloWorldBean ejbName: HelloWorldBean
      11:27:42,018 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

      HelloWorldBean/remote - EJB3.x Default Remote Business Interface
      HelloWorldBean/remote-ejb.server.HelloWorld.HelloWorldBeanRemote - EJB3.x Remote Business Interface
      HelloWorldBean/local - EJB3.x Default Local Business Interface
      HelloWorldBean/local-ejb.server.HelloWorld.HelloWorldBeanLocal - EJB3.x Local Business Interface




      When I run the client as a JAVA APPLICATION I get the output:
      Proxy to jboss.j2ee:jar=EJB.jar,name=HelloWorldBean,service=EJB3 implementing [interface ejb.server.HelloWorld.HelloWorldBeanRemote]
      Proxy to jboss.j2ee:jar=EJB.jar,name=HelloWorldBean,service=EJB3 implementing [interface ejb.server.HelloWorld.HelloWorldBeanRemote]
      Proxy to jboss.j2ee:jar=EJB.jar,name=HelloWorldBean,service=EJB3 implementing [interface ejb.server.HelloWorld.HelloWorldBeanRemote]

      What I am doing wrong that I dont get the object I want from the Container?



        • 1. Re: EJB 3.0 and JNDI problems
          peterj

          What object are you expecting? In EJB3, for a remote client, you will always get a proxy that implements the remote interface.

          By the way, you no longer need this code:

          HelloWorldBeanRemote beanH = (HelloWorldBeanRemote)PortableRemoteObject.narrow(bean, HelloWorldBe
          anRemote.class);

          You can instead do this:

          HelloWorldBeanRemote beanH = (HelloWorldBeanRemote)bean;

          Finally, you should not be using the HelloWorldBean in your client, you should use only the remote interface.

          • 2. Re: EJB 3.0 and JNDI problems

             

            "PeterJ" wrote:
            What object are you expecting? In EJB3, for a remote client, you will always get a proxy that implements the remote interface.

            By the way, you no longer need this code:

            HelloWorldBeanRemote beanH = (HelloWorldBeanRemote)PortableRemoteObject.narrow(bean, HelloWorldBe
            anRemote.class);

            You can instead do this:

            HelloWorldBeanRemote beanH = (HelloWorldBeanRemote)bean;

            Finally, you should not be using the HelloWorldBean in your client, you should use only the remote interface.


            Thanks for your reply Peter, it was really helpful.

            Since I am starting to study EJB 3 I still don't know what to expect and how the technology works yet.

            Hence, I don't know how to get the local interface in my client...could you please help me?

            best regards
            Gunnar

            I

            • 3. Re: EJB 3.0 and JNDI problems
              peterj

              You cannot get the local interface from a standalone client, only the remote interface. The local interface can be accessed only by a client, such as another EJB or a web app, running in the app server.