6 Replies Latest reply on Mar 15, 2010 12:20 PM by peterlen

    How can session beans share objects?

    peterlen

      Hello,

       

      I am having problems figuring out how to get two session beans to share an object.  The goal is to have Bean A declare an object, let Bean B fill the object's value, and then get Bean A to access to value set by Bean B.  I was looking at the @In and @Out SEAM annotations but have not fugured out if this will work.

      I have Bean A with:

      @Out private ExceptionMessageHandler messageHandler;
      public ClientUtilityBean() { messageHandler = new ExceptionMessageHandler(); }

      I have Bean B with:

      @In ExceptionMessageHandler
      messageHandler;

      Again, Bean A is the central point managing the messageHandler object and Bean B, Bean C, Bean D, etc would simply populate the object with a value.

      When I go to my page, however, I get the exception:

      15:48:25,499 ERROR [TxPolicy] javax.ejb.EJBTransactionRolledbackException: @In a
      ttribute requires non-null value:
      DataFolders.messageHandler

      where "DataFolders" is the interface for Bean B.   I have obviously not declared these correctly and may not be using the annotations properly in the first place.  I can't find any code examples that show what I am trying to do so I have been doing trial and error with no success.

       

      Does anyone know how I can properly use the @In and @Out annotations to work as I am intending or whether that is not the way to use the annotations and I need to do something else.

       

      Any thoughts would be greatly appreciated.

       

      Thanks - Peter

        • 1. Re: How can session beans share objects?
          amarkhel

          1. ExceptionMessageHandler has @Name annotation?

          2. You can use @In(require=false, create=true) ExceptionMessageHandler messageHandler;

          • 2. Re: How can session beans share objects?
            peterlen

            Andrey - Thanks for the reply.  I am unsure about what your reference to a @Name annotation is.  I don't list a @Name annotation.  Are you saying that I should?  For #2, I had declared it earlier like:

             

            @In(create=true) ExceptionMessageHandler messageHandler;

             

            But I get the same error.  I am wondering if both beans need the @In and @Out annotations but, again, an unsure about how these annotations really work in my scenario.

            • 3. Re: How can session beans share objects?
              amarkhel

              Every component, that you wan't put in any Context(request, session etc..) by @Out annotation, should have @Name annotation. Are your ExceptionMessageHandler bean has this annotation?

              And can you try @Autocreate annotation on your ExceptionMessageHandler? This annotation tell Seam, that component should be created by demand.

              And what scopes you use for all that beans(beanA, beanB, ExceptionMessageHandler)?

              • 4. Re: How can session beans share objects?
                peterlen

                Andrey,

                 

                This is where it is confusing to me.  Let me explain it a little better then I previous did.

                 

                Two of my beans (Bean A and Bean B) are session beans and each uses the @Name annotation.  Each of these beans are "connected" to my facelets page (my .xhtml file).  Depending on what the user does on the page, Bean A might be called or Bean B might be called.  These are typically a4j type calls.  I have a third bean which is also a session bean with a @Name annotation.  This third Bean (UtilityBean) is where I declared my

                 

                @Out

                 

                ExceptionMessageHandler messageHandler;

                 

                ExceptionMessageHandler is just a POJO with a couple of getters/setters.

                 

                My goal was trying to find a way that allowed these session beans to share a common object (ie the messageHandler).  I thought that the @In and @Out annotations were used for this purpose, but I am thinking that those annotations are for injection/outjection between a given bean and the client web page rather than for one bean to set a value on an object that is accessible in another bean.

                 

                Maybe I should never have listed the @In and @Out annotations in my original posting to keep any confusion out.  So, do you know if the @In and @Out annotations can be used to "pass" object values between session beans?  Essentially this would give access to one instance of a variable to different beans to set/get values.

                 

                - Peter

                • 5. Re: How can session beans share objects?
                  amarkhel

                  Hi, Peter.

                  To solve your problem your shouldn't use MessageNandler as one of fields of other session bean. Why you won't use messageHandler as undependent component(with @Name and @scope annotation)? Then you could inject it in all other beans through @In, and if you change state of messageHandler @Out annotation?

                  Simple Use case..

                  @Name("messageHandler")

                  @Scope(SESSION)

                  @AutoCreate

                  public class MessageHandler

                  ....

                   

                  @Name("beanA")

                  @Scope(SESSION)

                  BeanA{

                  @In MessageHandler messageHandler(only @In annotation if you won't change the state messageHandler)

                  ....

                  public String getInfo(){

                       return messageHandler.getInfo();

                  }

                   

                  @Name("beanB")

                  @Scope(SESSION)

                  BeanB{

                  @In @Out MessageHandler messageHandler(in case where you want change the state messageHandler)

                  public String getInfo(){

                       return messageHandler.getInfo();

                  }

                   

                  public void setInfo(String info){

                       messageHandler.setInfo(info);

                  }

                   

                  NOTE: @In and @Out annotation on class work after call on each method of this class.

                  So, after call beanB.setInfo(info), when you change state of messageHandler, @Out annotation push your messageHandler in session context.

                  • 6. Re: How can session beans share objects?
                    peterlen

                    Andrey,

                     

                    Part of my problem was not fully understanding the usage of the @In and @Out.  I just tried what you listed in your last reply and it works just fine.  I appreciate your time and effort in helping with this.

                     

                    Thanks - Peter