aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Calling EJB3.0 from another EJB3.0 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 » Java » EJB and other Java EE Technologies
Bookmark "Calling EJB3.0 from another EJB3.0" Watch "Calling EJB3.0 from another EJB3.0" New topic
Author

Calling EJB3.0 from another EJB3.0

Dhiren Lodhia
Greenhorn

Joined: Mar 05, 2008
Posts: 14

Hi all,
We all know that in EJB3.0 we can use @EJB annotaton to lookup the EJB either from Web Tier or application client or from another EJB also.all of these is working fine.

but EJB3.0 says that while calling EJB from another EJB it could also be done like following from one EJB3.0 :

@Resource private javax.ejb.SessionContext context;
public void myMethod() {
BankMgr bankMgr = (BankMgr)context.lookup(“ejb/BankMgr”);
}

Using SessionContext.
but when i try doing it i get an error saying NameNotFound for the 2nd EJB from 1st EJB??
no idea why???

apart from that how many names a EJB can have & if it has many then by what name should we look up??
for e.g
1)what is the name of EJB if we dnt give any name at all (nither JNDI nor mappedName)
2)what is mappedName (used with @Stateful & @Stateless)(after giving it can we lookup using old name)
3)what are implications of giving mappedName?
4)what is JNDI name for EJB?
5)how & where to give JNDI name to an EJB?(after giving it can we lookup using old name)
6)what is reference name for an EJB?(used in web tier in web.xml in <ejb-ref> element?
:roll: ops: :twisted:
Please help..............
I m using NetBeans6.5.10 & Glassfish V2

Thanks in advance.....................


Dhiren Lodhia
SCJP 5.0 (93%)
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Hi,

I will try to answer your questions the best as I can.

1. The default name of a session bean is the fully qualified class that it implements, i.e. SessionBean implements Foo.SomeRemoteInterface then that name attribute of SessionBean is bound to Foo.SomeRemoteInterface
2. mappedName bounds that session bean to a global variable in the RMI registry, but not all application server vendors are required to support this functionality
3. See #2 Assigning a mappedName may break portability across different application server vendors
4. See #1 - You can perform that following (assuming no ejb-jar.xml is used and default annotation values), then you can do the following



5. ejb-ref is that bound name for the session bean

For the purpose of SessionContext, you do not need to access this directly. If you really want to, the you can to modify the ejb-jar.xml to include the following information -



This deployment descriptor simply injects a session bean that implements the remote interface, bind to the the value of ejb-ref-name. Therefore, you can do the lookup on the SessionContext. I should note that the SessionContext is not aware of any global variables except for the ones declared in the ejb-jar.xml

I hope this information helps.



Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Dhiren,

You may also declare a component dependency using @Resource on the class. Kindly check with a good EJB 3.0 resource for futher details. The GlassFish docs, for example, are pretty decent.

Best regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Dhiren Lodhia
Greenhorn

Joined: Mar 05, 2008
Posts: 14
Jerwin Louise Uy wrote:Hi,

I will try to answer your questions the best as I can.

1. The default name of a session bean is the fully qualified class that it implements, i.e. SessionBean implements Foo.SomeRemoteInterface then that name attribute of SessionBean is bound to Foo.SomeRemoteInterface
2. mappedName bounds that session bean to a global variable in the RMI registry, but not all application server vendors are required to support this functionality
3. See #2 Assigning a mappedName may break portability across different application server vendors
4. See #1 - You can perform that following (assuming no ejb-jar.xml is used and default annotation values), then you can do the following



5. ejb-ref is that bound name for the session bean

For the purpose of SessionContext, you do not need to access this directly. If you really want to, the you can to modify the ejb-jar.xml to include the following information -



This deployment descriptor simply injects a session bean that implements the remote interface, bind to the the value of ejb-ref-name. Therefore, you can do the lookup on the SessionContext. I should note that the SessionContext is not aware of any global variables except for the ones declared in the ejb-jar.xml

I hope this information helps.




Thanks alot Jerwin
i did get some of doubts cleared specially about mappedName,i had thought it will be good to for ejb to give mappedName and will make ejb universally accessible from any where,didn't know that it is not in J2EE SPEC .
but i had a reason to believe that let me tell you one scenario ,
one i was trying to look up an EJB from remote standalone java POJO client and i kept getting the error of name not found till i gave the EJB mappedName.then it worked fine.

btw regarding SessionContext look up of EJB from EJB ,
i know that we don't need to do that we have many other ways but till i need to do it because its mentioned in Sun's EJB course FJ310 and hence i m required to show it practically to students.
i did read your post carefully and tried all you said but still it didn't work
i m posting here the code of mini project that i have made
please have a look at the code and let me know if any mistake is there ..........

i have made this Enterprise Application in NetBeans 6.1 and Glassfish AS
code of calling Second EJB from First using SessionContext is as follows :


please let me know if any thing more can be done..........
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Hi Dhiren,

Yes. That will cause the error because the deployment descriptor is wrong.

Here is the modified ejb-jar.xml



Regards.
Dhiren Lodhia
Greenhorn

Joined: Mar 05, 2008
Posts: 14
Jerwin Louise Uy wrote:Hi Dhiren,

Yes. That will cause the error because the deployment descriptor is wrong.

Here is the modified ejb-jar.xml



Regards.


Thanks a ton Jerwin,
it finally worked for me.
have no words to thank you,
will keep in touch with you .......
Regards Dhiren

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling EJB3.0 from another EJB3.0