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

    Re: Create stateless bean from stateful bean

    piotrekde Newbie

      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.)