5 Replies Latest reply on Jun 28, 2009 12:35 PM by Eric Taieb

    Question about Seam Events and components scope

    Eric Taieb Newbie

      Hi,


      I have been using seam for a few months. Until now I haven't used Events as I didn't really need them.


      Now I am trying to use them and I have a question I can't find an answer for (already read Seam in Action and various docs on the web).


      I have a Seam Component (JavaBean) with an application Scope. This component raises an event contentChanged.


      I have another Seam Componenet (JavaBean too) with a session scope. This component has a method that is an @Observer of the contentChanged event.


      If I have multiple sessions running do all the instances of the session scoped component receive the event ?


      If not (which I suspect) how do I broadcast an event to all the session scoped beans from a single application scoped bean ?



      Thanks in advance for your help....


      --eric

        • 1. Re: Question about Seam Events and components scope
          Leo van den berg Master

          Eric,


          The application scoped bean's events will be visible to all session scoped observers, but session scoped events aren't visible in other sessions.


          I use this construction in combination with ajax poll to inform all session about changes in the model (database).


          Leo

          • 2. Re: Question about Seam Events and components scope
            Eric Taieb Newbie

            I have tried it and only the component (session scoped) from the session that initiated the change receives the event raised by the application scoped bean.


            How can I produce a model with an application scoped bean that broadscasts events to all the instances of the session scoped bean ?


            Is this possible using Events ?




            Thanks in advance,


            --Eric

            • 3. Re: Question about Seam Events and components scope
              Eric Taieb Newbie

              Hi Leo,


              First thanks for your answer. This exactly what I expect but it doesn't work in my app...


              Let me be more precise describing my app and the problem I have:


              I have an application bean that holds a cached table (I didn't choose that design, I am expanding an existing webapp to use seam and I have to do with that, I can't use JPA entities). Lets call this bean the appCacheBean.


              I have 2 session beans:


              One session beans that commits data to the DB via the appCacheBean, lets call it commiterBean. In the appCacheBean, at the end of the commit, if it is successfull, I do a Events.raiseEvent(dataInserted)


              The other session bean that @Observes the dataInserted event and updates the user session UI. Lets call it observerBean.


              The problem I have is the following;


              I have 2 users (User1 and user2) connected (therefore 2 sessions). When User1 enters data, commiterBean for Users1 inserts the data through the appCacheBean. appCacheBean raises the event dataInserted and only observerBean for User1 receives the event.


              ObserverBean for User2 doesn't receive the event.


              If I swap Users and insert data with User2, only observerBean from user2 receives data.


              Weird...


              Do I need to specify something in the raiseEvent or the @Observer annotations as this clearly doesn't work as I expected or as you explain.


              Thanks for your help, it is greatly appreciated, I am really stuck on this...


              Cheers,


              --Eric


              • 4. Re: Question about Seam Events and components scope
                Eric Taieb Newbie

                I am desperate. I have tried everything and check with the debug.seam page that the scopes were right.


                I confirm, the event producer (the one that does the Events.raiseEvent) is Application Scoped


                and the other 2 beans (the one updating the cache and the one Observing the event) are session scoped.


                I even tried with forcing the Bijections using ScopeType.Application for the event Producer bean.


                I don't know why it doesn't work as I expect and as Leo described (weird, it seems to work for him and what he describes is exactly what I want to do)


                Is there anything else to explore ?


                What have I missed ???


                By the way I am running my webapp on Tomcat 6.0.20 with Seam 2.1.1GA


                Thanks anyway....


                Leo, can you help ? You seem to be the only one willing to reply... :-(


                • 5. Re: Question about Seam Events and components scope
                  Eric Taieb Newbie

                  After reading http://seamframework.org/Community/JMSMessageListenerInAWebApplication I came up with the conclusion that I had to use JMS to broadcast messages across multiple user sessions.


                  I have created 3 app scoped seam components:  MessageSession, MessagePublisher and MessageReceiver.


                  MessageSession:
                       -creates the jms connection through factory (jndi)
                       -creates session
                       -creates producer/topic
                       -creates consumer
                       -setsMessageListener to MessageReceiver


                  MessagePublisher:
                       -sends a message through the MessageSession


                  MessageReceiver:
                       -implements MessageListener and onMessage
                       -The onMessage method triggers an Events.raiseEvent


                  All init goes very well. JMS Messages are sent when data is updated. MessageReceiver treceives the message.
                  Now it seems that the onMessage method is running in a different context and the @Observers of the raised Events give null pointers for all injections.



                  Is this because I use WAR and Tomcat6 ?



                  Thanks for the help.