• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can EJB 2.1 call EJB 3.0 ?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Arun Yadav
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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">
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ralf for quick response. Appreciated.
 
Shivas ankar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

     
    reply
      Bookmark Topic Watch Topic
    • New Topic