The question is: this name always will be the same if I maintain the same EAR,JAR,Class and interface name? Or it can change from application servers?
JNDI names are not standardized in Java EE 5 and will change from one application server to the other. Adam Bien wrote a nice post illustrating this:
As I mentioned in my previous post,
the portability of Java EE 5
applications is much better, than in
the old J2EE 1.4 world. I found one
issue, which causes some effort - the
lack of defined and unified
JNDI-Naming and addressing. The
glassfish applicationsserver uses the
fully qualified name of the
remote business interface as default.
The JBoss appserver uses the name of
the EJB with the "/remote" ending. So
the following Session Bean:
package com.abien;
@Stateless
public class HelloWorldBean implements HelloWorld {
public String sayHello(String hello){
return "Echo from server: ";
}
}
can be found with JBoss (with EJB3
support) using the following
code-snippet:
Context context = new InitialContext();
HelloWorld helloWorld = (HelloWorld) context.lookup("myEarName/HelloWorldBean/remote");
and Glassfish (v1 and v2), using the
fully qualified name of the
remote-business interface:
Context context = new InitialContext();
HelloWorld helloWorld = (HelloWorld) context.lookup(HelloWorld.class.getName());
One decent way to handle this is to use a ServiceLocator
and "pluggable" application server specific strategies. Have a look at at ServiceLocator, JNDI Naming Helper and Java EE 5.
In Java EE 6, things are fixed and we finally have Portable Global JNDI Names.