8 Replies Latest reply on Jan 23, 2011 9:30 AM by thegroove

    Is it possible to attach an object to active stateless session bean

    thegroove

      Dear sirs,

       

      this question is EJB3 related and does not only regard JBoss.

      I would like to assign/register an object to a running method

      of a session bean.

       

      This object is session related and its  lifetime is limited to

      execution of a EJB method. During execution, any successive

      method call should be able to access this object without

      the need of passing it through the parameter.

       

      One may propose an associaive Array with the thread.id as a key

      and a object reference as a value. On JSF i would use a registered

      bean (request level).

       

      Does somebody has any idea how to provide this in a

      stateless session bean?

        • 1. Is it possible to attach an object to active stateless session bean
          ilya40umov

          1) Think about stateful ejb. I think that it should work for you if you don't need to get this object from another clients.

          2) I don't think that everything that works with static objects and thread is good in EJB. It won't work in a cluster. That's why I'm not sure that it's possible to share this object by reference with another threads.

          3) May be JbossCache could help you to store/access some temporal results of this method execution.

          • 2. Is it possible to attach an object to active stateless session bean
            thegroove

            Salut Ilya,

             

            thanks for your immediate reply.

             

            Maybe you misunderstood me or (possible) my description was not

            clear enough. The obhect should only be accessible whiile the

            session-ejb method is running. When the EJB invocation is finished

            an the control flow will go back to the caller, the assigned object

            gets invalid.

             

            There is nothing to save between two call,. therefore the stateful session

            bean would be a overcastet.

             

            =>1)

            The object might get assigned, when a request will call an EJB method.

            Image your EJB-method saveContract has been invoked. In series some

            other methods are called where one will try to get the assigned object

            by calling.

                 SessElm.getMyObject().

             

            In other words: You may also abuse me, not to  pass a ctx handle as

            a parameter for each subsequent method invocation !!!

             

            =>2) Since the assigned objects lifetime is limited to the duration of

            a EJB-sesssion-method, there is no demmand for a cluster safe solution.

             

            3) It should run also on other app servers

            • 3. Is it possible to attach an object to active stateless session bean
              ilya40umov

              1) How much time is your method going to be executed?

              2) Do you need this object to be accessible from another ejbs?

              3) Are you going to use this object as a status of your method execution?

               

              P.S. EJB is designed to be thread safe and easy for clustering. I think that your solution is going to break these things. And even if you will find a workaround I guess that it can cost you a lot of time in the future.

              • 4. Is it possible to attach an object to active stateless session bean
                thegroove

                Salut Ilya

                 

                > 1) How much time is your method going to be executed?

                frequently

                 

                > 2) Do you need this object to be accessible from another ejbs?

                 

                no. The object assignment is needed for one method. The method

                is acting like a basket, where subsequent calls of that method put

                in an drop some things.

                Outside of the bean and outside of this window this specific object

                assinment ist not used.

                 

                >3) Are you going to use this object as a status of your method execution?

                >it is only used inside the execution of the methode, so it does not have

                >a state in the way you would have one wiht stateful session beans.

                 

                > P.S. EJB is designed to be thread safe and easy for clustering. I think

                > that your solution is going to break these things. And even if you will

                > find a workaround I guess that it can cost you a lot of time in the future.

                 

                It does not, cause the object assignment will be dissolved when the

                control flow leaver my code and will never be reestablished.

                • 5. Is it possible to attach an object to active stateless session bean
                  ilya40umov

                  Let me show you a very simple example:

                  @Stateless

                  class A {

                  someMethod() {

                  ...

                  }

                  }

                   

                  @Stateless

                  class B {

                  @EJB

                  private A a;

                  doTest() {

                  a.someMethod();This calls someMethod of stateless bean A.

                  a.someMethod();This MAY call someMethod of another bean A.

                  a.someMethod();This MAY call someMethod of another bean A.

                  }

                  }

                   

                  So this means that stateless bean does not have any state. So after you called any method once you definetly won't see this object/bean again.

                  Please put into my sample what you want to happen and where.

                  • 6. Re: Is it possible to attach an object to active stateless session bean
                    thegroove

                    Salut Ilya,

                     

                    its much easier:

                     

                         @Stateless(....)

                         public class MyBean

                         {

                           final private static     Logger            LOG = Logger.getLogger(MyBean.class) ;

                     

                           public int myMethod(...)

                           {

                             final CallCtx    myCtx = new CallCtx() ;

                             {

                                          myCtx.add(somewaht) ;

                                  

                     

                               callLibAMethod1(.....)

                     

                     

                               callLibBMethod2(.....)

                     

                               .....

                             }

                             finally

                             {

                               myCtx.releaseAll() ;

                             }

                           }

                         }

                     

                    where:

                     

                     

                     

                         class LibA

                         {

                           methode1()

                           {

                             getRegObject("somewhat")

                             ...

                             callsSomeoneElse(...)

                           }

                     

                         }

                     

                         class LibB

                    /* similar to LibA */

                     

                    The simple question is: Will i find some tricky workarouds, that prevents

                    me of adding a myCtx param to each of the subsequently called methode,

                    that may be interestes in the registered object.

                     

                    The registered object is used similar to a JSF-Bean with the exception,

                    that it is not used outside from myMethod. Therefore, any solution

                    will be cluster-safe cause the object will be initialized on each new request

                    is will be not shared across two ejb-method calls.

                     

                    So, a node globel memory would be OK, where each session/thread

                    id would identiy a slot in that node-global memory. Anyway i would like

                    to use a more EJB related sollution, if there exist one.

                     

                    Another solution woluld be a parameter filed at each of the subsequently

                    called methode, passing a ctx-memory located at the bean.


                    • 7. Re: Is it possible to attach an object to active stateless session bean
                      ilya40umov

                      1) IMHO The style of programming you are going to use is not a functional programming and thus I don't think that it's appropriate for Enterprise Applications where a lot of programmers have to modify/understand the same code.

                      2) I think that the best way outs for you are(one of them):

                      - use a something similar to http://download.oracle.com/javase/1.4.2/docs/api/javax/security/auth/callback/CallbackHandler.html

                      - pass the current EJB bean through this reference

                      • 8. Re: Is it possible to attach an object to active stateless session bean
                        thegroove

                        Salut Ilya,

                         

                        1) I do not believe in getting any price for good code-design. The code is

                        actually there and i need to get it into an EJB. My jobs is to find the best

                        trade-off of a given initial position.

                         

                        2) I'll think about this.

                         

                        Thanks for the discussion