aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB, JNDI & ENC - real life scenario Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJB, JNDI & ENC - real life scenario" Watch "EJB, JNDI & ENC - real life scenario" New topic
Author

EJB, JNDI & ENC - real life scenario

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Hi

I am preparing for the EJB certification and I am going thro the EJB 3.1 book (O'Reilly)

One of the chapters discusses about JNDI, ENC and the EJB connections

Can some of you give me a real life scenario of these so that I can get a better understanding


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Ok, no one really replies, so I will try to...

You mean a real life example of why do you need the JNDI ENC when defining a reference to another EJB / resource / persistence unit / etc.?

The below post is just my point of view - it doesn't have to (in any matter) reflect how it really was.

I guess it's because of the backward compatibility.
I can just suspect that in former days there wasn't an easy way to access the 'injected' EJB reference from another EJB. That's why they need to define (in DD) in what JNDI location the reference will be available for the given EJB.
Right now you can still define in what ENC JNDI context you would like to put your injected EJB reference to be able to look it up directly in one of your business methods. Then again, I guess that nowadays you can use the @EJB, @Resource, etc. (not mentioning the CDI!) so I can't think of of any real use of the directly accessing the ENC using JNDI aside from integrating with some legacy code.

Did it help you in any way?

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

I don't remember the contents of that chapter. But since you are asking about ENC and the JNDI names, I think this previous discussion might help.

[My Blog] [JavaRanch Journal]
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

One thing to clarify this.

Jaikiran, in this post I can read that:
Each application deployed in the application server's JVM will have its own java:/comp/env jndi namespace.


Is this (still?) true? Isn't the ENC a 'personal' naming space for an EJB and not for the whole application?

Thanks in advance!

Cheers
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

That could have been better worded in my other post. In EE6, each application (a .ear application for example) will have a separate java:app namespace, each module (ex: a EJB jar module or a web app module) a separate java:module namespace and each component (for example a EJB) will have a java:comp namespace.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Ok, and just to have the full picture, let's fully clarify this. According to:

EJB 3.1 spec wrote:A .jar file in WEB-INF/lib that contains enterprise beans is not considered an independent Java EE “module” in the way that a .war file, stand-alone ejb-jar file, or an .ear-level ejb-jar file is considered a module. Such a .jar does not define its own module name or its own namespace for ejb-names, environment dependencies, persistence units, etc. All such namespaces are scoped to the enclosing .war file. In that sense, the packaging of enterprise bean classes in a WEB-INF/lib .jar is merely a convenience. It is semantically equivalent to packaging the classes within WEB-INF/classes.


and

EJB 3.1 spec wrote:In a .war file, there is a single component naming environment shared between all the components (web, enterprise bean, etc.) defined by the module. Each enterprise bean defined by the .war file shares this single component environment namespace with all other enterprise beans defined by the .war file and with all other web components defined by the .war file.
The Bean Developer should be aware of this name scoping behavior when selecting names of environment dependencies for enterprise beans packaged within a .war file. Unlike enterprise beans packaged in an ejb-jar file outside a .war, names of environment entries defined by an enterprise bean inside a .war can clash with names defined by other components. Likewise, enterprise beans packaged in a .war file have visiblity to all environment entries defined by any other components in the .war file, including any entries defined within web.xml.


So if you wrap your EJB's with web application in *.war file, then the java:comp/env namespace will be used for all EJB's within the *.war file, but if you would wrap the application as an independent ejb-jar *.jar file, the java:com/env namespace will be a private space for each EJB.

Is this correct?

Cheers!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

So if you wrap your EJB's with web application in *.war file, then the java:comp/env namespace will be used for all EJB's within the *.war file,

The java:comp will be shared by all components (including EJBs and servlets and other components) if the components are packaged in .war file.


but if you would wrap the application as an independent ejb-jar *.jar file, the java:com/env namespace will be a private space for each EJB.

That's correct.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks!

I guess that now we just need to wait for the OP to see if this thread cleared a bit the JNDI and ENC topics :-)

Cheers!
 
Don't get me started about those stupid light bulbs.
 
subject: EJB, JNDI & ENC - real life scenario