File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes ejb-local-ref and ejb-ref Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "ejb-local-ref and ejb-ref" Watch "ejb-local-ref and ejb-ref" New topic
Author

ejb-local-ref and ejb-ref

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>StatelessSBBean</ejb-name>
<ejb-local-ref>
<ejb-ref-name>aaa</ejb-ref-name>
<injection-target>
<injection-target-class>com.mellon.StatelessSBBean</injection-target-class>
<injection-target-name>refLocal</injection-target-name>
</injection-target>
</ejb-local-ref>
</session>
</enterprise-beans>
</ejb-jar>

Believe it or not, the above descriptor works well. Here are code lines of my bean:

@Stateless
public class StatelessSBBean implements StatelessSBLocal {
private ReferencedLocal refLocal;

public void business(){
refLocal.method();
}
}

O'REILLY book "Enterprise Java Beans 3.0", says that <ejb-ref-name>, <ejb-ref-type>, <remote> are required.

"The <ejb-ref> element defines a reference to remote EJBs. It contains the subelements <description> (optional), <ejb-ref-name>
(required), <ejb-ref-type> (required), <remote> (required), <home> (optional), <ejb-link> (optional), and <mapped-name> (optional), as
well as the element <injection-target> (optional) described in the first part of this chapter."

I checked the relevant parts in JSR 220 Page 417:

"The ejb-ref-name element specifies the EJB reference name: its value is the environment entry name used in the enterprise bean code. The ejb-ref-name must be specified. The optional ejb-ref-type element specifies the expected type of the enterprise bean: its value must be either Entity or Session. The home and remote or local-home and local elements specify the
expected Java types of the referenced enterprise bean�s interface(s). If the reference is to an EJB 2.1 remote client view interface, the home element is required. Likewise, if the reference is to an EJB 2.1 local client view interface, the local-home element is required. "

Book is wrong again?


SCJP,SCWCD1.3,SCWCD1.4,SCJD,SCBCD5,SCEA5
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
It is going complicated.

If I comment the injection-target element,
<enterprise-beans>
<session>
<ejb-name>StatelessSBBean</ejb-name>
<ejb-local-ref>
<ejb-ref-name>aaa</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.mellon.ReferencedLocal</local>
<!--
<injection-target>
<injection-target-class>com.mellon.StatelessSBBean</injection-target-class>
<injection-target-name>refLocal</injection-target-name>
</injection-target>
-->
</ejb-local-ref>
</session>
</enterprise-beans>
</ejb-jar>

the three element are exactly required, omiting anyone will bring errors.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Mellon Sun,

think about it: When injecting an ejb 3 bean the container can recover the business interface from the type of the instance variable placed in the <injection-target-name> element. Without injection this isn't possible.

By the way: If you use @EJB on class level, the field "beanInterface" has to be filled, too.


SCJP 5 (98%) - SCBCD 5 (98%)
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
It seems that ejb-ref and ejb-local-ref are like resource-ref, the ref-type element is optional if an injection target is specified for the resource.
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
When I was writing testing code for ejb-local-ref, I encountered a funny thing.

I defined a ejb-local-ref like:
<enterprise-beans>
<session>
<ejb-name>StatelessSBBean</ejb-name>
<ejb-local-ref>
<ejb-ref-name>asdf</ejb-ref-name>
<local>com.mellon.ReferencedLocal</local>
</ejb-local-ref>
</session>
</enterprise-beans>

My EJB :

public class StatelessSBBean implements StatelessSBLocal {
//@EJB(name="asdf")
@Resource(name="asdf")
private ReferencedLocal refLocal;

public void business(){
refLocal.method();
System.out.println("This is Stateless Session Bean bussiness method.");
}
}

At first time, I wrongly injected the referenced EJB with @Resource, GlassFish V2 + NetBeans6.5 output stacktraces when the application was deploying:

javax.naming.NameNotFoundException: asdf not found
at com.sun.enterprise.naming.TransientContext.doLookup(TransientContext.java:216)
at com.sun.enterprise.naming.TransientContext.lookup(TransientContext.java:188)
at com.sun.enterprise.naming.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:74)
at com.sun.enterprise.naming.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:111)
...

Bug I can call the method business() via a servlet, and the referenced EJB's method is called and the expected message was printed too.

ReferencedBean's method.
This is Stateless Session Bean bussiness method.

If I modify the name attribute of @Resource from "asdf" to "asdf2" or any other things, deploy process generate same exception stacktraces. However, I cannot run the servlet as expected anymore.
[ December 10, 2008: Message edited by: Mellon Sun ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ejb-local-ref and ejb-ref