Where is the JNDI name in my code?

Rohit Elayathu picture Rohit Elayathu · Mar 16, 2012 · Viewed 15.8k times · Source

I have created a EJB2.0 using Eclipse 3.7 IDE, and deployed it in JBoss 5 application server (my bean name is product). I am doing normal context lookup (and other stuff to call ejb), and I am able to call EJB successfully. Now my question is what is JNDI name exactly, and where did it get used in all this. Is my bean name the JNDI name, or is this my JNDI name -> org.jnp.interfaces.NamingContextFactory. Where is the JNDI name in this????? my code:-

// initial code.............
Context  ctx = getContext();
Object obj=ctx.lookup("Product");
ProductHome home =(ProductHome)  javax.rmi.PortableRemoteObject.narrow(obj,ProductHome.class);
ProductRemote remote=home.create();

Product prd = new rohit.Product("PRDCamera",001,50.50) ;
remote.addProduct(prd);
remote.updateProduct(prd);
remote.removeProduct(001);
remote.findProduct(001);
remote.findAllProduct();


// getContext Method

public static InitialContext getContext() throws Exception{
    Properties pro = new Properties();
    pro.put(javax.naming.InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
    pro.put(javax.naming.InitialContext.PROVIDER_URL,"localhost:1099");
    return new InitialContext(pro);
}

Answer

Ingo Kegel picture Ingo Kegel · Mar 16, 2012

There is no JNDI name in your code.

This is how you look up EJBs in EJB 2.0:

Object ejbHome = initialContext.lookup("java:comp/env/com/mycorp/MyEJB");

MyHome myHome = (MyHome)javax.rmi.PortableRemoteObject.narrow(
  (org.omg.CORBA.Object)ejbHome, MyHome.class);

The JNDI name is java:comp/env/com/mycorp/MyEJB in this case.

In the much saner EJB 3.0, you just do

MyEJB myEJB = initialContext.lookup("java:comp/env/com/mycorp/MyEJB")

and do away with the terrible home interface idea.