7 Replies Latest reply on Oct 17, 2010 4:38 AM by Kinga Szabo

    Raise Events in all Sessions

    Peyman Rezaei Newbie

      Hi,


      Is it possible to use an event that calls an observer of all sessions?


      I have a session component like this and i want to update all logged in persons after a special transaction. How can I do it?


      
      @Out(required=false, ScopeType.SESSION)
      @In(required=false, ScopeType.SESSION)
      public Person loggedInPerson;



      I tried with @Observer, but I could only call the Observers of the current session, in which the transaction had happened. So i couldn't update the other loggedIn persons. Any idea?
      Thanks

        • 1. Re: Raise Events in all Sessions
          Leo van den berg Master

          Hi,


          There is no direct way to do this, but you can create a component with application scope which receives the original event and than relay it to the session scoped beans through another event.



          Leo.

          • 2. Re: Raise Events in all Sessions
            Peyman Rezaei Newbie

            Hi Leo,


            I tried your suggestion, but relaying of an event didn't call observers in other sessions. I tried the following example:




            @Scope(ScopeType.APPLICATION)
            public class ApplicationEventHandler{
            
            
                @Observer("person.changed.session")
                public void relayEvent(){
                 Events.instance().raiseEvent("person.changed.application");
                }
            
            }
            
            @Scope(ScopeType.SESSION)
            public class SessionBean{
            
                @Out(required=false)
                @In(required=false)
                public Person loggedInPerson;
            
                @Observer("person.changed.application")
                public void updateLoggedInPerson(){
                    loggedInPerson.update();
                }
            
                public void updateAllLoggedInPersons(){
                 Events.instance().raiseEvent("person.changed.session");
                }
            
            }
            
            



            Is that what you meant?

            • 3. Re: Raise Events in all Sessions
              Leo van den berg Master

              Hi,


              The way you're doing it won't work because the bean instance which receives the event is NOT THE SAME as the one which processes it. You can see this with a simple log in the oebserver method. Sorry I wasn't a bit more clear about how you can do this trick!


              First of all you want to have an overview of all loggedIn persons. The only way to have a common picture of logged in persosn is to create such an overview at Application Scope, so every bean which needs this overview can see this.


              I suggest more or less the following (needs extra work!!!!:




              @Name("userOverView")
              @Scope(Application)
              class  UserOverview
              
              private Set<User> loggedInUser = new HashSet<User>();
              
              @Unwrap
              public Set<User> getLoggedInUsers(){
               return loggedInUsers;
              }
              
              @Observer("Get your add event here")
              public void addUser(User u){
              }
              @Observer("Get your remove event here")
              public void removeUser(User u){
              }
              ....
              ....
              }
              
              
              You can inject the components in the other components without a problem and you will alsways have the last state of the logged in users. Mind you it takes some additional work to remove the user, because that is bit more trickier when the user just closes the browser, but you could add (1) add a ping/alive task to every page, (2) observe the logout event, (3) observe destroying of session scoped components.
              
              Hopefully this helps.
              Leo
              
              
              
              
              



              • 4. Re: Raise Events in all Sessions
                Kinga Szabo Newbie

                Hi!


                I'm working on exactly this problem.
                In my application I have to update all the logged in users about certain changes.
                After some research on this forum I found a suggestion to store a list of sessions. I guess it serves the same purpose as storing an overview of the logged in users.
                The question is how do you send the event to these sessions/users?



                Kinga

                • 5. Re: Raise Events in all Sessions
                  Peyman Rezaei Newbie

                  @Leo: Thanks alot. I collected all session components in a application-scoped list and then updating worked fine for every session.


                  @Kinga: Did you store session beans in your list? from which scope tye do you want to send your events to sessions?

                  • 6. Re: Raise Events in all Sessions
                    Kinga Szabo Newbie

                    Hi!


                    In my application scoped list I stored the actual sessions of the users (HttpSession), but I couldn't do much with them. I thought I could just somehow send an event to all the sessions from my list. That clearly doesn't work.


                    In my case I want to send an event from a Conversational Bean to all the other Conversational Beans from all active the sessions.
                    Example: Users are editing a product list. One of them changes an entry. Then each logged in user should be informed about the changes.


                    The solution is to store my product list in an application scoped list and ...how to notify the logged in users about changes in my product list?


                    • 7. Re: Raise Events in all Sessions
                      Kinga Szabo Newbie

                      Ok, I think I've figured it out. I have to store the Beans in my application scoped list.So in my case the ProductManagerBeans and not the actual Product list.


                      Thanks for the helping questions!:)