13 Replies Latest reply on Feb 26, 2009 10:03 PM by Stuart Douglas

    What is difference bewteen SLSB scope and SFSB.EVENT scope?

    Gus Gu Expert

      Hi everyone,


      When studying seam examples, I found some of them use SLSB to retrieve/display data, and some use SFSB.EVENT scope to retrieve/display data. When I changed SLSB to SFSB.EVENT, or SFSB.EVENT to SLSB, it still works. I want to know what functionality difference bewteen them, especially for a real/enterprise project. In other word, when should use SLSB scope, and when should use SFSB.EVENT? Thank you in advance.

        • 1. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
          James Kelly Newbie

          I may be misunderstanding the question, but it sounds like you're mixing the notions of Seam contexts and Seam components.


          The Event context is essentially the life of a single web request or more eloquently, a generalization of the notion of the web request context to cover other kinds of events. More here


          Stateful and stateless session beans are supported component types. Their differences are well documented in the Seam Reference:



          Stateless session bean components are not able to hold state across multiple invocations. Therefore, they usually
          work by operating upon the state of other components in the various Seam contexts. They may be used as
          JSF action listeners, but cannot provide properties to JSF components for display.

          Stateful session beans do maintain state:



          Stateful session beans are often used as JSF action listener, and as backing beans that provide properties to JSF
          components for display or form submission.

          Each component type has a default context which can be overridden in the component:



          When no scope is explicitly specified, the default depends upon the component type. For stateless session
          beans, the default is STATELESS. For entity beans and stateful session beans, the default is CONVERSATION.
          For JavaBeans, the default is EVENT.

           

          • 2. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
            Sebastien Degardin Newbie

            Hi,


            The big difference is that the SFSB in Event context will span the lifeCycle of a JSF request (like the request context) and will be destoyed at the end of the request.


            I would recommend to use SFSB for most case.


            SLSB is really useful to encapsulate reusable code (like DAO for example).


            Hope it helps !

            • 3. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
              Gus Gu Expert

              Hi James and Sebastien,


              Thank you so much for your excellent explanation. They are very helpful.

              • 4. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                sridhar kondoji Newbie

                Hi Sebastien,
                Letme rephrase Shen G's question as iam in the same boat with confusion.


                What is the purpose of SFSB componenet which is event scoped? We could use SLSB instead?
                If i understand correctly, SFSB compoenet type will loose its state as soon as the event is served.
                I am referring to Booking example that comes with seam. In that example, the code author has used RegisterAction SFSB componenet which is EVENT scoped.


                Thanks
                sri

                • 5. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                  Stuart Douglas Master

                  Stateless session beans will not remember field values between calls (well they might sometimes, but if you are relying on this behavior then this is a bug). For instance the following contrived example will not count the number of calls to a method during a request because there is no guarentee that you will get the same SLSB:


                  class myComponent
                  {
                     @In CallCounterLocal callCounter;
                  
                    public void doStuff()
                    {
                       callCounter.countCall();
                       //do stuff
                  
                    }
                  
                  }
                  
                  @Name("callCounter")
                  @Stateless
                  class CallCounter implements CallCounterLocal
                  {
                    int callCount = 0;
                  
                    public void countCall()
                    {
                      callCount++;
                    }
                  
                  } 
                  
                  



                  however the following will work:



                  class myComponent
                  {
                     @In CallCounterLocal callCounter;
                  
                    public void doStuff()
                    {
                       callCounter.countCall();
                       //do stuff
                  
                    }
                  
                  }
                  
                  @Name("callCounter")
                  @Statefull
                  @Scope(ScopeType.EVENT)
                  class CallCounter implements CallCounterLocal
                  {
                    int callCount = 0;
                  
                    public void countCall()
                    {
                      callCount++;
                    }
                  
                    //@Remove etc
                  
                  } 
                  
                  



                  • 6. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                    sridhar kondoji Newbie

                    Hi Stuart,
                    In your SFSB example above, will not seam create Statefull callCounter for each event?
                    If i understand the componenets and contexts correctly, the callCounter Statefull componenet should be destroyed once Event is served and returned to the view. Am i wrong?


                    If that is correct callCount should always display 1 for each countCall() method call.
                    Thanks for your answer.
                    --sri

                    • 7. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                      Stuart Douglas Master

                      No. ScopeType.EVENT lasts for the span of the request, so if you call it twice in the request it will display 2. If you are using a stateless session bean the results are undefined. I am not sure if stateless components are created new for each call or if seam uses instance pooling, so I am not 100% sure what the behaviour would be with stateless pojo's.


                      Stuart

                      • 8. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                        sridhar kondoji Newbie

                        Hi Stuart,
                        Within the same request call, if you call 2 times the method countCall() with SFSB, yes the count will be 2. However,what will be the value of variable callCount if a second request is made from SFSB componenet in your example?


                        I should try this myself, but iam new to SEAM, and also to J2EE. I am just geting started by reading some books.
                        It will be fewmore days before i get started coding. However questions like above are confusing me.



                        I know that EVENT context lasts for the span of the request. But, what will happen to componenets stored in event context after the request is served?
                        Thanks
                        sri

                        • 9. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                          Stuart Douglas Master

                          After the event the event context is destroyed, and the objects will eventually be garbage collected (unless you have done something silly, like saved a direct reference to them somewhere).


                          To recap:


                          Stateless - should not store any state, you should treat these like you are getting a new object every request (due to instance pooling this may not be the case).


                          Event - The event context is created at the begining of the request and is destroyed once the request is finished, taking all event scoped components with it.


                          • 10. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                            sridhar kondoji Newbie
                            Hi Stuart,
                            Thanks for the recap. This will bring us backto the original question.
                            Whats the point of the statefull session bean whose scope is event in the following booking example class.

                            If this SFSB object is going to be destroyed as soon as event context is destroyed, there is no point in using SFSB here. right?

                            @Stateful
                            @Scope(EVENT)
                            @Name("register")
                            public class RegisterAction implements Register
                            {
                               @In
                               private User user;
                              
                               @PersistenceContext
                               private EntityManager em;
                              
                               @In
                               private FacesMessages facesMessages;
                              
                               private String verify;
                              
                               private boolean registered;
                              
                               public void register()
                               {
                                  if ( user.getPassword().equals(verify) )
                                  {
                                     List existing = em.createQuery("select u.username from User u where u.username=#{user.username}")
                                        .getResultList();
                                     if (existing.size()==0)
                                     {
                                        em.persist(user);
                                        facesMessages.add("Successfully registered as #{user.username}");
                                        registered = true;
                                     }
                                     else
                                     {
                                        facesMessages.addToControl("username", "Username #{user.username} already exists");
                                     }
                                  }
                                  else
                                  {
                                     facesMessages.addToControl("verify", "Re-enter your password");
                                     verify=null;
                                  }
                               }
                              
                               public void invalid()
                               {
                                  facesMessages.add("Please try again");
                               }
                              
                               public boolean isRegistered()
                               {
                                  return registered;
                               }
                               public String getVerify()
                               {
                                  return verify;
                               }
                               public void setVerify(String verify)
                               {
                                  this.verify = verify;
                               }
                              
                               @Remove
                               public void destroy() {}
                            }

                            Thanks
                            sri
                            • 11. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                              Stuart Douglas Master

                              For that example you could use a POJO. Event scoped POJO's make sense some times, for instance if you need to use @PersistenceContext or @TransactionAttributeType(REQUIRES_NEW).


                              • 12. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                                sridhar kondoji Newbie

                                Thank you Stuart for answering patiently to us.
                                --sri

                                • 13. Re: What is difference bewteen SLSB scope and SFSB.EVENT scope?
                                  Stuart Douglas Master

                                  Sorry, my last post should have read:


                                  For that example you could use a POJO. Event scoped SFSB's make sense some times, for instance if you need to use @PersistenceContext or @TransactionAttributeType(REQUIRES_NEW).