users@glassfish.java.net

Re: Using EJB 3.0 session bean from J2EE 1.4 web project

From: <glassfish_at_javadesktop.org>
Date: Tue, 02 Sep 2008 12:44:52 PDT

Hi Ryan,

The reason you haven't seen it anywhere is that the spec doesn't require that EJB 3.0 business interfaces be directly available to pre-Java EE 5 components. If the web application must be J2EE 1.4 style your only portable option is to expose an EJB 2.x "adapted" LocalHome view from your EJB 3.0 bean. A bean can expose multiple views so you don't have to remove whatever other business interface(s) the bean already implements. Since the web app is in the same .ear you will then have access to the 2.x LocalHome view of the target bean.

Do the following :

1) implement the LocalHome and Local interfaces according to the old EJB 2.x rules

2) Add a @LocalHome(My2xLocalHome.class) annotation on the stateless session bean class.
Note that you use the 2.x LocalHome interface, not the 2.x Local interface, in the annotation. The Local interface is derived from the create method on the LocalHome so it doesn't need to appear anywhere on the bean class itself.

3) Add an ejb-local-ref in the web application's web.xml to create an local ejb dependency on the target 2.x LocalHome/Local view of the stateless session bean. Put the beanName of the target stateless session bean in the ejb-link element. The beanName is either the value of the @Stateless name() attribute or if that's not set the *un*qualified name of the bean class.

  <ejb-local-ref>
      <ejb-ref-name>my_slsb</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <local-home>com.acme.My2xLocalHome</local-home>
      <local>com.acme.My2xLocal<local>
      <ejb-link>MySlsb</ejb-link>
  </ejb-local-ref>

4) Look up the reference in the web component's environment naming context. E.g. if the ejb-ref-name of the ejb-local-ref is my_slsb the lookup would be

   My2xLocalHome mlh = (My2xLocalHome) new InitialContext().lookup("java:comp/env/my_slsb");

The client has no idea the bean is implemented using the 3.0 API so it completely follows the 2.x client programming model :

   My2xLocal my2xLocal = mlh.create();

   ...


Here are some other resources to look at on these topics :

http://blogs.sun.com/enterprisetechtips/entry/ejb_3_0_compatibility_and

https://glassfish.dev.java.net/javaee5/ejb/examples/Adapted.html

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#EJB_ejb-ref_ejb_local_ref

 --ken
[Message sent by forum member 'ksak' (ksak)]

http://forums.java.net/jive/thread.jspa?messageID=296762