EJB - Lookup failed for 'ejb/BookRequestBean'
Asked Answered
I

4

7

I am new to EJB, and was trying "Hello World" type of EJB Java program. Here is my EJB:

package dukesbookstore.ejb;
@Stateless(name="BookRequestBean", mappedName="ejb/BookRequestBean")
@Named
public class BookRequestBean {
    //Other codes here
}

and here is my client:

    Properties prop = new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
    prop.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
    prop.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
    try {
        InitialContext ctx = new InitialContext(prop);                              
        ctx.lookup("ejb/BookRequestBean");
        System.out.println("EJB Look-up successfull!!");
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But whenever I try to run, I am getting below exception:

javax.naming.NamingException: Lookup failed for 'ejb/BookRequestBean' in SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory, org.omg.CORBA.ORBInitialHost=localhost, java

I have added appserv-rt.jar , gf-client.jar, javaee.jar, but still no luck. Can anyone help me, what I am missing here? I am usign Glassfish 3.1

Inspissate answered 10/5, 2013 at 11:27 Comment(1)
Full stacktrace would help. Make sure you also look at this : glassfish.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJBJablon
D
2

In addition to nice @RaviTrivedi answer, here are few thoughts:

  • @Named annotation shouldn't be used this way
  • don't use both name and mappedName, for Glassfish it is enough to use just mappedName
  • your EJB should implement remote interface
Deliquescence answered 13/5, 2013 at 7:29 Comment(2)
Slight interjection. name is used to identify EJB whereas mappedName is EJB's JNDI name. Also mappedName is vendor specific. ie: Glassfish supports, Websphere not.Immense
@RaviTrivedi Yep, that's why I noted that it's enough for Glassfish. It shouldn't be used if you want fully portable applications.Deliquescence
I
8

There can be several reasons to this:

1) Your EJB is not mapped to JNDI name. You need to check whether your EJB is deployed successfully and it is mapped to JNDI name. You can check Server GUI, Server Log on startup or use Universal Test Client to see if EJB is mapped correctly. Note, UTC will only show Remotely exposed EJBs.

2) Your EJB is only exposed to Local application. In this case, Remote call or Cross application call(different EAR, WAR...) to your EJB will fail. In this scenario, create Remote interface and expose it. Local interface exposes EJB to only local calls. Remote interface exposes EJB to remote or cross application calls.

3) Your RMI/IIOP port may be incorrect. You can check Glassfish GUI or Server startup log to see what port RMI/IIOP is assigned to.

Note: To diagnose the exact problem, please post full stack trace.

Immense answered 12/5, 2013 at 9:30 Comment(0)
D
2

In addition to nice @RaviTrivedi answer, here are few thoughts:

  • @Named annotation shouldn't be used this way
  • don't use both name and mappedName, for Glassfish it is enough to use just mappedName
  • your EJB should implement remote interface
Deliquescence answered 13/5, 2013 at 7:29 Comment(2)
Slight interjection. name is used to identify EJB whereas mappedName is EJB's JNDI name. Also mappedName is vendor specific. ie: Glassfish supports, Websphere not.Immense
@RaviTrivedi Yep, that's why I noted that it's enough for Glassfish. It shouldn't be used if you want fully portable applications.Deliquescence
B
2

Adding to @Ravi Trivedi and @Miljen Mikic, if you are using Glassfish, you should check how your EJB is registered in JNDI. In Glassfish for example type the following command:

 asadmin list-jndi-entries
Boundary answered 14/3, 2014 at 1:29 Comment(0)
P
0
1. your above code works perfectly on glassfish only missing was the remote interface.
2. As suggested above mappedname[vendor specific] and name ....yada yada.....
3. copy below code and run you should be good to go
4. only ensure the *client*.jar is on your path and redeploy the application to glassfish server and run main.
**This Remote interface (the only addition to your above code);**        
            import javax.ejb.Remote;
            
            @Remote
            public interface BookRequestI {
                //Other codes here
                String getISBN();
            }
            
            **your existing implementation spiced with my getISBN() to prove the point :)**
            
            import javax.ejb.Stateless;
            
            
            @Stateless(name="BookRequestBean", mappedName="ejb/BookRequestBean")
            public class BookRequest implements BookRequestI {
                //Other codes here
                @Override
                public String getISBN(){
                    return "ISBN 87 - 11 - 07559 - 7";
                }
            }
            
            **your test as is with my getISBN and typing to interface Type.**
            
            import javax.naming.Context;
            import javax.naming.InitialContext;
            import javax.naming.NamingException;
            import java.util.Properties;
            
            public class BookRequestT {
            
                public static void main(String[] args) {
                   Properties prop = new Properties();
                    prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
                    prop.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
                    prop.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
                    try {
            
                        Context ctx = new InitialContext(prop);
                        BookRequestI bookRequest = (BookRequestI) ctx.lookup("ejb/BookRequestBean");
                        System.out.println("EJB Look-up successfull!!" +  bookRequest.getISBN());
                    } catch (NamingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            
                }
            }
            
            output:
            EJB Look-up successfull!!ISBN 87 - 11 - 07559 - 7
            
            Process finished with exit code 0
Prestissimo answered 18/7, 2020 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.