3 Replies Latest reply on Dec 30, 2010 7:30 AM by jaikiran

    Re: Create stateless bean from stateful bean

    piotrekde

      Thanks for answer!

      Never instantiate EJBs (or any server managed components) via the constructor invocation. Doing so will strip off all the server provided services off those components (for example, the instances will no longer be having any transaction or security semantics).

      I didn't know it, I'll take it into account in future.

      You mean same JVM? I would say it doesn't make any difference, but I haven't yet understood the question, so I might be missing something.

      Yes, that's what I mean. Please take a look at the example:

       

      Here we've got Stateless bean which is responsible for doing some computations (running on JBoss):

       

      @Stateless
      public class WorkerBean implements WorkerRemote {
        public void doSomeComputations(int x) {
          // executing this method takes some time
        }
      }
      

       

      This is ordinary Java class which runs on my Desktop JVM. It runs 100 times computations, each one with different argument.

      public class ClientPOJO {
        public static void main(String[] args) {
          Context jndiContext = getInitialContext();
          for(int i=0; i<100; i++) {
            WorkerRemote r = (WorkerRemote) jndiContext.lookup("WorkerBean/remote");
            r.doSomeComputations(i);
          }     
      }
      

       

      This code results that stateless beans are taken from the pool. So it might be 1,5,10 or even 20 WorkerBean objects (it depends on JBoss).

      Now I want to do the same what do ClientPOJO#main but using Stateful bean running on JBoss.

      The question is: how to obtain references to WorkerRemote interface appropriately?

       

      @Stateful
      public class Manager implements ManagerRemote {
        public void orderComputations() {
          for(int i=0; i<100; i++) {
            WorkerRemote r = // how to obtain reference? Should I use JNDI as ClientPOJO or @EJB annotation?
            r.doSomeComputations(i);
          }
        }
      

       

      (This is not real logic - actually method orderComputations() will be called X times, but I think that this code clearly describes my question.)

        • 1. Re: Create stateless bean from stateful bean
          jaikiran
          WorkerRemote r = // how to obtain reference? Should I use JNDI as ClientPOJO or @EJB annotation?

          You can use @EJB injection since you are using it within another server side component (i.e. the @Stateful bean). However, that doesn't mean that you cannot use JNDI lookup. You can use JNDI lookup too, but @EJB injection is a convenient way of getting hold of the bean.

          • 2. Re: Create stateless bean from stateful bean
            piotrekde

            Thanks Jaikiran! I didn't know that @EJB gives reference backed by stateless beans pool - I thought that's just single stateless bean injection.

            • 3. Re: Create stateless bean from stateful bean
              jaikiran

              piotrekde wrote:

               

              I didn't know that @EJB gives reference backed by stateless beans pool - I thought that's just single stateless bean injection.

              Yeah, an @EJB internally injects a proxy similar to what you would have got when you do a JNDI lookup. And it's only when you invoke on that SLSB, that the instance association happens. It's the same irrespective of whether you use JNDI lookup or @EJB injection.