1 Reply Latest reply on May 26, 2011 12:49 PM by wolfgangknauf

    EJB3 annotation question ...

    toddler

      Hi ...

      I am new to JBoss, EJB and EJB3 ...on a steep learning curve...

       

      We are moving to JBoss 5.1.0 from 4.3.0 and as part of this would like to move from EJB to EJB3.  I just read the following article: "EJB 3: From legacy technology to secret weapon" by Adam Bien here:

      http://www.javaworld.com/javaworld/jw-10-2008/jw-10-ejb3.html

       

      For our EJB to EJB3 change, I've investigating using annotations as described in the above article. In our existing EJB implementation, we create the remote interface, home interface, bean class, deployment descriptors (ejb-jar.xml, jboss.xml), etc.

       

      In the above article, it talks about using Declarative injection for the client, using

       

      @EJB

      private Service service;

       

      and the container injects the ClientBean, etc...

       

      Our client implementation is multi-threaded, performs async requests and uses a per package InterfaceMgr that creates a separate bean per thread (uses a MAP ...).

      We use stateful sessions.  Had to do this as got EJBExceptions related to a Stateful bean being used with a different tx context (async requests via thread pool)....

       

      So when a bean needs to be used the one doesn't existing for the thread/package, we create a new bean using the "Home".create() call. Code snippet ...

       

        Object lNarrowTo = initialContext_.lookup(EJB_NAME);

        MYCLASSHome = (EjbMYCLASSHome)             PortableRemoteObject.narrow(initialContext_.lookup(EJB_NAME),

                          EjbMYCLASSHome.class);

      ...

              bean = (EjbMYCLASS) PortableRemoteObject.narrow(

                      MYCLASSHome.create(...),

                      EjbMYCLASS.class);

       

      Is it possible to create a new bean when required using an annotation, when we don't have the "Home" interface ...?  If so, how?

       

      The idea being using annotations for everything and not manually (via tools) creating the remote interface, home interface, bean class, deployment descriptors (ejb-jar.xml, jboss.xml), etc. as we currently do.  I know we can continue to use all these artifacts, but ...

       

      Any suggested articles, tutorials, books I should read to come up to speed on this technology...

      Thanks...

        • 1. Re: EJB3 annotation question ...
          wolfgangknauf

          Hi,

           

          as you wrote, "Home interfaces" are gone in EJB 3.x.

          Bean instances are created whenever needed: if using an "@EJB" annotation, the server will create a new bean (or take an unused instance from the pool). If you inject a stateful session bean, the server will also take care to restore the state from some previous session.

           

          And if you perform a JNDI lookup, you will also get a fresh instance of the bean. I think that each lookup of a stateful bean will kill its state - so if you need a stateful bean, store it in some membervariable on the client side after the initial lookup.

           

          Hope this helps

           

          Wolfgang