Adam Bien's Weblog
EJB 3 Portability Issue: why JNDI names are not standardized?
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("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());
I prefer the Glassfish style, because it fits better into the convention over configuration idea of the EJB3 spec. If you have a remote interface in the classpath, you can access it directly, using it's fully qualified name (typos are already recognized by the compiler...).
In the JBoss case you have to read the doc - which is not very intuitive, the names are not unique (the JNDI-names of beans from different packages would be the same), but the local and remote access can be better distinguished.
I really do not understand, why the JNDI naming schema is not standardized across the servers. I would prefer the Glassfish style, but regardless which strategy is used, it should be defined in the spec. The fully qualified name of the remote/local-interface would be absolutely sufficient...
Posted at 12:36PM Dec 12, 2006 by Adam Bien in Java / JEE / Architecture | Kommentare[10]
[my tweets]
Rss My book: Real World Java EE - Rethinking Best Practices


Totally agree that it should be standard, I've been yammering at Sun to fix this for 2 years.
Your JBoss example is incorrect, it should be "myEarName/HelloWorldBean/remote". I don't think it is at all common to have beans with the same name in different packages in the same EAR.
BTW, the GlassFish approach is a very bad one, since if I deploy the same EAR twice, or two EARs with the same EJB JAR, I will get my beans bound to the same names. The whole *point* of having an appserver is you can isolate different deployments.
Gesendet von Gavin King am December 13, 2006 at 05:06 PM CET #
Hi Gavin,
thank you for your comment. Should we start a JSR for this? :-)
I forgot in the JBoss case the prefix - you are right. But if you deploy only the ejb-jar - there is no prefix. Also in Glassfish case a defined prefix or "namespace" could fix the problem.
"I don't think it is at all common to have beans with the same name in different packages in the same EAR."
Just try to deploy the JBoss-EJB-Tests and you will run into that issue:-)
thank you very much for your comment,
adam
Gesendet von Adam Bien am December 14, 2006 at 12:13 PM CET #
Actually, JBoss is even better than I said. That is not the classname in the JNDI name, it is the EJB name, which is required by the spec to be unique in an ejb-jar. So there is actually no possibility of a name clash at all!
Well, unless you deploy the ejb-jar outside of an EAR. Most people use EARs in practice.
I think we will be able to fix this in the next rev of EJB.
Gesendet von Gavin King am December 14, 2006 at 03:14 PM CET #
Hi Gavin,
yes, you are right. The EJB-Name is derived from the Bean name per default. If you provide one, it has impacts to the JNDI name.
"Well, unless you deploy the ejb-jar outside of an EAR. Most people use EARs in practice."
Yes except, your persistence-tests :-)
thank you for your comment!,
adam
Gesendet von Adam Bien am December 20, 2006 at 10:53 AM CET #
Adam,
I absolutely agree with you. Right now I'm working with 3 different ejb containers (Glassfish, JBoss and OpenEjb) and they use totally different conventions for ejb names. It's incredible, isn't it?
Gesendet von Filippo am December 24, 2006 at 09:25 AM CET #
The Jboss approach does make more sense from a 'multiple implementations, one remote interface' approach.
I'm still getting frustrated/confused between the different naming, particuarly when dealing with WAR accessing remotely, locally, from outside an EAR, from within an EAR.
There should be some specification action quickly on this, and hurting the 'portability' cause significantly.
Gesendet von dhartford am March 22, 2007 at 03:06 PM CET #
Excellent post on the variations in vendor implementations of global JNDI names. Sadly, we'll never see any spec action on this; or rather we already have, the j2ee application client is the offical answer.
It should be noted that the OpenEJB style of defining a JNDI name can be changed to just about any format you like. You supply a template, we fill in the values:
http://openejb.apache.org/jndi-names.html
Gesendet von David Blevins am September 22, 2007 at 02:42 PM CEST #
Many thanks for your useful insights.
Had been banging my head against a brick wall trying to understand why JNDI lookup in JBoss was not working whilst following along with examples in APress's Beginning Java EE 5 Platform (Chapter 9)
Gesendet von Jeremy Flowers am March 06, 2008 at 01:23 PM CET #
This is a the most useful article on the internet, ever. I m trying "run first EJB" very long time and, I had a lot of trouble with JDNI names, deploying and loong configuration. Your Bean.clas.getName() ond ClassFish works fine and firstly for ME, very thx. Oh man, thx. This article helped me to break bar between "reading" and "working" - Marta Jandova bless you !
Gesendet von AZOR am May 10, 2008 at 01:53 AM CEST #
How do you find the session bean if deployed to a tomcat 5.x server?
Gesendet von Viggo am February 23, 2009 at 06:09 PM CET #