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 and other Java EE Technologies and the fly likes can EJB 2.1 call EJB 3.0 ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "can EJB 2.1 call EJB 3.0 ?" Watch "can EJB 2.1 call EJB 3.0 ?" New topic
Author

can EJB 2.1 call EJB 3.0 ?

Arun Yadav
Greenhorn

Joined: Mar 14, 2003
Posts: 19
Hi,

Is it possible to invoke EJB3 from EJB2.1, both ejb modules belong to same ear. I need to invoke an EJB3 from ibm webshpere startup bean which is implemented as EJB2.1.

Thanks in advance.


Arun (SCJP-1.4, IBM-141, SCWCD-1.4)
"How hard you try is rooted to how often you try" - Doug Hall"
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
Yes it is possible to invoke EJB3 from EJB2.1. The older EJB applications can be deployed as is on EJB3.0 container without rewriting them. Because pre-EJB3.0 APIs are available in EJB3.0. Also a bean written against EJB3.0 APIs can service clients written to use earlier versions of EJB APIs.
Arun Yadav
Greenhorn

Joined: Mar 14, 2003
Posts: 19
Thanks Shahnawaz,

I am very happy to know this, but I am wondering the EJB2.1 expects home interface in order to get hold of bean component. But since EJB3 doesn't have home interface how EJB2.1 can access EJB 3.0

e.g: If one EJB2.1 wants to access other EJB2.1 the lookup code would be
something like this:




Now if other(called) EJB is EJB3.0, what would be the <EJB>Home in above example. As per my knowledge the EJB3.0 doesn't expose home interface.

I am relatively new to EJB stuff, so please disregard my understanding if its incorrect.

Thanks in advance.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30293
    
150

Originally posted by Arun Yadav:
Now if other(called) EJB is EJB3.0, what would be the <EJB>Home in above example. As per my knowledge the EJB3.0 doesn't expose home interface.

You are correct that EJB 3 does have a home interface.

Take a look at this example from "EJB 3 in Action"


The JNDI lookup uses an EJB reference on PlaceBid. This would be defined in the deployment descriptor for your EJB 2.X bean. Then you do a cast directly to the bean type.

On a side note, why do you want to call the remote interface for the EJB 3 bean? You are in the same ear/JVM so a local interface should suffice.

You can lookup any bean 2.X/3.0 from a POJO along with any other Java class (including another bean.) The only differences lie in setting up the ejb reference (to the JNDI name.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Arun Yadav
Greenhorn

Joined: Mar 14, 2003
Posts: 19
Thanks Jeanne,

It really worked, I am able to call EJB3.0 from EJB2.1 directly accessing the local interface. No home is requried.

The websphere by default created the local JNDI name as ejblocal:<packageName>.<LocalInterfaceName> for EJB3 component, And I got the reference as below:



Thanks again.
Shree Shreeni
Greenhorn

Joined: Jan 12, 2010
Posts: 3
Hi Arun, can you share the ejb-local-ref that you defined for referring ejb3 beans from ejb2.1 beans.

I tried using something like
<ejb-local-ref>
<ejb-ref-name>ejb3refname</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>ejb3localinterface</local>
<ejb-link>module#ejb3bean</ejb-link>
</ejb-local-ref>

and I get an error for the missing <local-home> during deployment.
SAXParseException:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'local'.
One of '{"http://java.sun.com/xml/ns/j2ee":local-home}' is expected.

btw, I am using the following xsd for ejb2.1 module

<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version="2.1">
Ralf Pantförder
Greenhorn

Joined: May 10, 2010
Posts: 8
There is no <ejb-local-ref> in the above example. Instead, it uses the global JNDI name to lookup the bean's local interface. In a WebSphere AS, the default global JNDI name of an EJB 3.0 local interface is ejblocal:fully_qualified_classname.

The following helper method performs the global JNDI lookup, hiding the naming convention from client code:

It is used without type-cast in the following manner:

This is the most convenient way to lookup an EJB 3.0 from within EJB 2.1 code. However, it makes use of a proprietary naming convention. If you don't like this, or if you insist on using solely the environment naming context ("java:comp/env") for all lookups, you are in fact required to ad hoc define within your EJB 3.0 two additional EJB-2.1-like interfaces: (1) a local object, i.e. an interface extending javax.ejb.EJBLocalObject (not to be confused with the EJB3 local business interface), and (2) a local home, i.e. an interface extending javax.ejb.EJBLocalHome, specifying a standard create() method. These interfaces must then be referenced in the mandatory <local> and <local-home> sub-elements of the <ejb-local-ref> within your EJB 2.1 component.
Shivas ankar
Greenhorn

Joined: Jun 11, 2008
Posts: 3
Hi,
I am using @Singleton bean. How can I access from ejb2.1 session bean? See code below.

package org.siva.in;

@Singleton
@Remote(SingleRemote.class)
@Local(SingleLocal.class)
@Startup
public class Single implements SingleRemote, SingleLocal {


Note: 1-Should I go for GLOBAL_JNDI or LOCAL JNDI?
Note: 2- what could be the fully qualified name based on the above package?

URGENT please.

--Siva.
Ralf Pantförder
Greenhorn

Joined: May 10, 2010
Posts: 8
From my point of view, this should work exactly as in my above example, i.e.:

with Method NamingUtil.lookupEjbLocal(Class) defined as above.

Use global lookup, not local! For being able to locally lookup your bean, you would need to pollute you EJB 3.0 code with EJB-2.1-like interfaces (see above).

In your example, the global JNDI name ist ejblocal:org.siva.in.SingleLocal.
Shivas ankar
Greenhorn

Joined: Jun 11, 2008
Posts: 3
Thanks Ralf for quick response. Appreciated.
Shivas ankar
Greenhorn

Joined: Jun 11, 2008
Posts: 3
Hi Ralf,
I followed the same instructions. Steps below.

1. added ejb3.1 singleton bean.jar as part of build path in ejb2.1 session bean
2. Imported the package.SingleLocal.class
3. added the above code with new InitialContext().lookcup(ejblocal:myEAR.ear:myEJB.jar#);
4. ctx.lookup(package.SingleLocal.class.getName();

Status: my ejb2.1 unable to deploy by saying error while deploying the ejb2.1 bean

Note: I didn't do any changes in any of configuration.xml files. Please advice.

--Siva.
Ralf Pantförder
Greenhorn

Joined: May 10, 2010
Posts: 8
Suppose you have an EJB3 named MyBean in a module MyEJB.jar in an application MyApp. Suppose this bean has local and remote interfaces mypackage.MyBeanLocal and mypackage.MyBeanRemote. WebSphere then binds the local interface to two locations (see the server log output during startup):

  • ejblocal:mypackage.MyBeanLocal
  • ejblocal:MyApp/MyEJB.jar/MyBean#mypackage.MyBeanLocal

  • The former is the one I used in my above example. Employing the latter binding location, the lookup code would read something like this:

    But I don't see a need for this excess of complexity.

    Likewise, WebSphere binds the remote interface to the following locations:

  • mypackage.MyBeanRemote
  • ejb/MyApp/MyEJB.jar/MyBean#mypackage.MyBeanRemote

  • Again, I would preferably use the simpler name for the lookup:

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: can EJB 2.1 call EJB 3.0 ?