Search code examples
ejbjndi

Lookup an EJB Implementation by jndi name


I am new to the jndi namespace and I am trying to lookup my EJB class from a service class through below code

InitialContext ctx;
        try {
            ctx = new InitialContext();
            ctx.lookup("?????");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

But I really don't have any clue what to put inside lookup. I know that jndi name goes there. But from where can I get the jndi name for my ejb class. I am new to this thing and really can't devote much time in learning JNDI.


Solution

  • Refer server log file. When EJb beans are deployed. It looks something like this

    JNDI bindings for session bean named SampleImpl in deployment unit subdeployment "sample-web-1.0.0.war" of deployment "xyz.ear" are as follows:

    Sample code to lookup an EJB bean.

    final Hashtable jndiProperties = new Hashtable();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    final Context context = new InitialContext(jndiProperties);
    SampleIf sif = (SampleEJBIf) context.lookup("java:app/sample-ejb-1.0-SNAPSHOT/SampleImpl");
    

    Other ways to define Lookup path

    java:global/pms/pms-web-1.0.0/SampleClass!com.etipl.pms.x12.SampleClass
    java:app/pms-web-1.0.0/SampleClass!com.etipl.pms.x12.SampleClass
    java:module/SampleClass!com.etipl.pms.x12.SampleClass
    java:global/pms/pms-web-1.0.0/SampleClass
    java:app/pms-web-1.0.0/SampleClass
    java:module/SampleClass