1 2 Previous Next 20 Replies Latest reply on Apr 6, 2009 4:48 PM by alrubinger

    JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5

    yetti

      Hello,

      I've got a question that I think someone can help me with pretty quickly. I've got a stateless session bean that I've created based on this tutorial:

      http://www.myeclipseide.com/documentation/quickstarts/ejb3/

      My project name is different but the architecture of the project is the same -> a package that contains the bean, an interface, a local and remote. Then I have a simple tester app where I'm trying to call the bean using the remote.

      JBoss starts just fine using both the command line as well as starting from within Eclipse. The bean deploys just fine either with a restart of JBoss or a redeploy. Where I am having trouble is running the tester application, specifically getting the JNDI name correct. When I set up the project I did NOT do support for Entity beans/JPA. Below is a snippet of the log from JBoss starting/deploying the bean:

      ----------------------------------------
      11:20:06,218 INFO [JBossASKernel] Created KernelDeployment for: com.acmeco.serverapp.ftp
      11:20:06,234 INFO [JBossASKernel] installing bean: jboss.j2ee:jar=com.acmeco.serverapp.ftp,name=FTPBean,service=EJB3
      11:20:06,234 INFO [JBossASKernel] with dependencies:
      11:20:06,234 INFO [JBossASKernel] and demands:
      11:20:06,234 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
      11:20:06,234 INFO [JBossASKernel] and supplies:
      11:20:06,234 INFO [JBossASKernel] Class:com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal
      11:20:06,234 INFO [JBossASKernel] jndi:FTPBean/local-com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal
      11:20:06,234 INFO [JBossASKernel] jndi:FTPBean/remote-com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote
      11:20:06,234 INFO [JBossASKernel] jndi:FTPBean/local
      11:20:06,234 INFO [JBossASKernel] jndi:FTPBean/remote
      11:20:06,234 INFO [JBossASKernel] Class:com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote
      11:20:06,234 INFO [JBossASKernel] Added bean(jboss.j2ee:jar=com.acmeco.serverapp.ftp,name=FTPBean,service=EJB3) to KernelDeployment of: com.acmeco.serverapp.ftp
      11:20:06,312 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=com.acmeco.serverapp.ftp,name=FTPBean,service=EJB3
      11:20:06,328 INFO [EJBContainer] STARTED EJB: com.acmeco.serverapp.ftp.ejb3.FTPBean ejbName: FTPBean
      11:20:06,406 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

      FTPBean/remote - EJB3.x Default Remote Business Interface
      FTPBean/remote-com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote - EJB3.x Remote Business Interface
      FTPBean/local - EJB3.x Default Local Business Interface
      FTPBean/local-com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal - EJB3.x Local Business Interface

      -----------------------------------

      Here is the code in the testing client app:


      package com.acmeco.serverapp.ftp.ejb3;
      
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      public class FTPBeanTestClient {
      
       /**
       * @param args
       */
       public static void main(String[] args) {
       // TODO Auto-generated method stub
       try {
       InitialContext ctx = new InitialContext();
       FTPBeanRemote bean = (FTPBeanRemote) ctx.lookup("com.acmeco.serverapp.ftp.ejb3.FTPBean/FTPBean");
       bean.doWork();
       } catch (NamingException e){
       e.printStackTrace();
       }
       }
      
      }
      


      I know I'm doing something dumb with the InitialContext.

      1. What should I be putting into the client app context lookup?
      2. Am I doing something else wrong/dumb?

      Thanks in advance,
      yetti

        • 1. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
          yetti

          By the way, here is the error message I get when I run the console app:

          ---------------------------
          javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
          at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
          at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
          at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
          at javax.naming.InitialContext.lookup(Unknown Source)
          at com.acmeco.serverapp.ftp.ejb3.FTPBeanTestClient.main(FTPBeanTestClient.java:15)
          ---------------------------

          • 2. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
            peterj

            One way to determine the JNDI name assigned to anything is to use JNDIView within the jmx console to view the names. Then you can look up the EJB using the correct name.
            http://www.jboss.org/community/docs/DOC-9583

            I would guess that you want to look up the name "FTPBean/remote"

            • 3. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
              yetti

              Peter,

              Thanks for replying. Here's the tree I get doing what you suggested:

              +- FTPBean (class: org.jnp.interfaces.NamingContext)
              | +- local (class: Proxy for: com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal)
              | +- remote (class: Proxy for: com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote)
              | +- remote-com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote (class: Proxy for: com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote)
              | +- local-com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal (class: Proxy for: com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal)


              So, I've tried everything I think listed above in my InitialContext with no success (same error as above). What am I doing stupid? :)

              Thanks in advance,
              yetti

              • 4. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                peterj

                Did you read the second paragraph of my earlier reply?

                • 5. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                  peterj

                  One other thought - what is in your jndi.properties file, and is that file in your classpath?

                  • 6. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                    yetti

                    Yes I read the second paragraph, and several times (not being a smart ass, trying to fix my problem! :) )

                    FTPBean/remote isn't listed (as apparent from the snippet I posted earlier), however, I've tried:

                    com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal
                    com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote
                    remote-com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote
                    local-com.acmeco.serverapp.ftp.ejb3.FTPBeanLocal

                    Am I missing some context declaration thing? I'm new at this, but perhaps something like FTPBean/remote/com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote or something?

                    I am learning JBoss as I go on a pet project. As soon as I can get this hello world demo going I plan to do some serious study. Thanks for replying.

                    yetti

                    • 7. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                      yetti

                       

                      "PeterJ" wrote:
                      One other thought - what is in your jndi.properties file, and is that file in your classpath?


                      I don't believe I have a jndi.properties file in the project.

                      com.acmeco.serverapp.ftp
                      +src
                      -+com.acmeco.serverapp.ftp.ejb3
                      ---FTPBean.java
                      ---FTPBeanTestCliebt.java
                      ---FTPBeanLocal.java
                      ---FTPBeanRemote.java
                      ---IFTP.java
                      -+META-INF
                      ---MANIFEST.MF
                      -+JRE System Library
                      -+Java EE 5 Libraries
                      -+Referenced Libraries
                      ---(I included all JAR's in the jboss/client/ folder)

                      • 8. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                        peterj

                        The name "FTPBean/remote" is listed:

                        +- FTPBean (class: org.jnp.interfaces.NamingContext)
                        | +- remote (class: Proxy for: com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote)


                        The standard jndi.properties file is:

                        java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                        java.naming.provider.url=jnp://localhost:1099
                        java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
                        


                        It needs to be in the classpath for your client.





                        • 9. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                          peterj

                          Looks like your Eclipse projects contains both the EJB and the client. Make sure that you do NOT package the jndi.properties file into the EJB JAR file that gets deployed to JBoss AS.

                          • 10. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                            yetti

                            Peter,

                            So would it be easier for me to do a new project for the client, stuff the jndi.properties file in there and build it accordingly? Sorry about the n00b questions, but the hello world tutorials on Jboss/EJB3 aren't exactly the greatest.

                            yetti

                            • 11. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                              peterj

                              I often mix client and server code in the same project in Eclipse, but then I use Ant for packaging and deployment which lets me build things as I see fit.

                              But you could create a separate client project and have it depend on the EJB project.

                              • 12. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                                yetti

                                Very cool. Thanks Peter for the help, I will give that a whirl. Once I get it built, I'd like to turn it into a good hello world so that others can get up to speed quickly.

                                • 13. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                                  yetti

                                  Very cool. Thanks Peter for the help, I will give that a whirl. Once I get it built, I'd like to turn it into a good hello world so that others can get up to speed quickly.

                                  • 14. Re: JNDI Issue - Deploying EJB3 using Eclipse to JBoss 5
                                    yetti

                                    Okay, I'm about to drink a pint of windshield wiper fluid. If Peter or anyone else can help me, I'm forever grateful.

                                    I have created a new standard Java project, named com.acmeco.serverapp.ftp.client, that sits next to the EJB3 app in my Eclipse workspace.

                                    I have created a jndi.properties file in the build path that has the data Peter suggested (even though I'm already hardcoding this info in the class itself below).

                                    I have created a new class in the default package called HelloClientPOJO.java. Here is the code:

                                    import javax.naming.InitialContext;
                                    import javax.naming.Context;
                                    import javax.naming.NamingException;
                                    import java.util.Properties;
                                    import javax.rmi.PortableRemoteObject;
                                    
                                    import com.acmeco.serverapp.ftp.ejb3.*;
                                    
                                    public class HelloClientPOJO {
                                    
                                     FTPBean _ftpbean;
                                    
                                     public HelloClientPOJO(){
                                     try{
                                     Context jndiContext = getInitialContext();
                                     Object ref = jndiContext.lookup("com.acmeco.serverapp.ftp.ejb3.FTPBeanRemote");
                                     _ftpbean = (FTPBean)ref;
                                     }catch (javax.naming.NamingException ne){
                                     //nothing
                                     }
                                     }
                                    
                                     protected void sayHello() {
                                     _ftpbean.doWork();
                                     }
                                    
                                     public static void main(String [] args)
                                     {
                                     HelloClientPOJO helloClient= new HelloClientPOJO();
                                     helloClient.sayHello();
                                     }
                                    
                                     // developed for JBoss only. this is vender dependency
                                     public static Context getInitialContext( ) throws javax.naming.NamingException {
                                     Properties p = new Properties( );
                                     p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                                     p.put(Context.URL_PKG_PREFIXES, " org.jboss.naming:org.jnp.interfaces");
                                     p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
                                     return new javax.naming.InitialContext(p);
                                     }
                                    }
                                    


                                    Here is the error on the console when the POJO app runs:
                                    Exception in thread "main" java.lang.NullPointerException
                                    at HelloClientPOJO.sayHello(HelloClientPOJO.java:24)
                                    at HelloClientPOJO.main(HelloClientPOJO.java:30)


                                    I got this from this URL/tutorial:
                                    http://biese.wordpress.com/2008/02/20/how-to-call-ejb3-from-jsp-servlet-and-stand-alone-application/

                                    Neither the server nor the bean(s) have been changed, so my JBoss startup/deployment log and JNDI info is all the same as before. What am I doing wrong? What do I need to put into the content lookup to find the bean?

                                    Please help! Once I get this working, I am going to write the world's most idiot proof Hello World for JBoss 5/EJB 3/Eclipse. I know a LOT of other people have/had problems with these issues. I want to end the confusion once and for all.

                                    yetti

                                    1 2 Previous Next