7 Replies Latest reply on Jul 29, 2009 1:02 AM by asookazian

    Factory method is getting called so many times

    hubaghdadi

      Hey,


      I noted that a factory method is getting called so many times upon the page request (according to log statements).


      Is it the norm situation?


      Thanks.

        • 1. Re: Factory method is getting called so many times
          asookazian

          What does page request mean exactly?  Submission of a form (HTTP POST request) or actually PAGE scope?  Show code.


          The @Factory method fires when the associated @DataModel List object, for example, is null.  So if your List is going out of scope (and being destroyed by Seam container) or your Seam component is going out of scope and being destroyed, it's possible that the @Factory method will fire again.  It happens also when you programmatically set the List object to null.

          • 2. Re: Factory method is getting called so many times
            cash1981

            Maybe its because of the different JSF stages?

            • 3. Re: Factory method is getting called so many times
              asookazian

              The multiple calls to getter methods from rendered attributes applies to the above comment (JSF life cycle phases) but I don't think it's applicable for @Factory.


              It's a matter of is the value for @Factory null or not.  If it's null, the method will (re-)execute.  Very simple.

              • 4. Re: Factory method is getting called so many times
                hubaghdadi
                @Name("groupManager")
                @Scope(ScopeType.CONVERSATION)
                public class GroupManager implements Serializable {
                
                    @Logger
                    private Log logger;
                
                    @In
                    private EntityManager entityManager;
                
                    @DataModel
                    private List<Group> groupList;
                
                    @SuppressWarnings("unchecked")
                    @Factory("groupList")
                    public void loadGroups() {
                        logger.info("Load Groups");
                        groupList = entityManager.createQuery("select grp from Group grp").getResultList();
                        logger.info("Done #{groupList}");
                    }
                    
                }



                The Group component is registered under the event scope.

                • 5. Re: Factory method is getting called so many times
                  accless

                  u never know how much/often the rendered-Attribute of jsf-componentns is called during the render-response-phase of the jsf-life-cylce.
                  U have to create some kind of cache. for your case u could achive this easily by doing this:




                   @SuppressWarnings("unchecked")
                      @Factory("groupList")
                      public void loadGroups() {
                          logger.info("Load Groups");
                          if (groupList == null) {
                               logger.info("refresh groupList from db");
                               groupList = entityManager.createQuery("select grp from Group grp").getResultList();
                          }
                          logger.info("Done #{groupList}");
                      }
                  



                  • 6. Re: Factory method is getting called so many times
                    kukeltje.ronald.jbpm.org

                    That should not be needed for @Factory annotated methods afaik. I think I read somewhere that there might be a 'clash' by having the datamodel field also named 'grouplist'.


                    And try changing the


                    logger.info("Done #{groupList}");



                    to


                    logger.info("Done #0", groupList");




                    • 7. Re: Factory method is getting called so many times
                      asookazian

                      It is unnecessary to check for null with a @Factory method if you're simply checking the same variable.  The method will exec if the associated List is null.  Thus, the check is not required.


                      I do not see an @Begin anywhere in that component, is an LRC already running?  If not, the component will be destroyed by Seam after it has rendered the JSF page (all temp conversations are cleaned up by Seam - add an @Destroy method to prove it).