9 Replies Latest reply on Aug 7, 2009 4:58 PM by Brandon Simpson

    Visits counter

    Francis Drake Newbie
      I'd like to make visits/referers logger. I've created filter for *.seam requests and a class for saving data into DB. However, saving to DB each time new visitors comes could end up in making my site inaccessible. I decided then to gather records in list, and when it's size is 20 then save all of them into DB.

      The problem is I am not sure how to create object that is going to keep the list. I tried with Application-scoped class, but then when I try to get it in my filter, I have no active application context :(

      How could I solve above problem?
        • 1. Re: Visits counter
          Khaled Yousfi Newbie


          take a look at the seam documentation to see how to create an application scoped componenent.
          you will have to use an annotation like this :

          @Scope(APPLICATION) @Startup
          public class VisitCounterComponent{

          by the way you could also use the interceptors: My Link

          • 2. Re: Visits counter
            Francis Drake Newbie

            but the problem is not creating of app-scoped component but accessing it in the filter. When I try in session bean org.jboss.seam.contexts.Contexts.getApplicationContext().get(MyClass.class) I can access my component, but in filter org.jboss.seam.contexts.Contexts.getApplicationContext() returns null.

            That's why I ask, how to make workaround for this problem, with this interceptors thing? Or I've read also about context-filters, but they are only defined for servlets and I have no idea how could I use it in this situation.

            • 3. Re: Visits counter
              Leo van den berg Master

              is it not easier to log data in a specific session-scoped bean which will be auto-created on creating the session, or maybe simpler adding an event everytime a specific page is viewed. These events can be observerd through @Observer) and that observer bean can be used for logging.

              • 4. Re: Visits counter
                Francis Drake Newbie

                could be. but what I am concerned about is situation when I have big traffic on site. That's why I wanted one common place in memory to record visits/pages/referers and when fx list grows to 50 elements, then persist all of them to database.

                In your solution, which is interesting, I'm afraid I have to save to db for each user separately. Perhaps it could somehow expanded?

                • 5. Re: Visits counter
                  Leo van den berg Master


                  Sending an event to a specific bean doesnt mean you have to save it directly. You could save it in memory and every time you exceed a specific amount of events you save it to disk. I would definiely increase the number of records and add a time limit of an hour (suppose its sunday morning and you won't have a lot of visitors)  You can add whatever kind of Object to the parameterList of an event so you can do whatever you want with the observer which basically provides an component independent way of inetraction between independent beans.

                  The most important advantages I see is that you won't interfere with the Seam-cycle with an additional Filter and you can stick to a 100% Seam solution.

                  I've made a number of applications with additional servlets/beans etc. but i am more-and-more trying to reduce this to a minimum at the moment because of maintenance.


                  • 6. Re: Visits counter
                    Francis Drake Newbie

                    I got your idea and I've read some on this matter. I think I will go this way, but it has one weak point (hasn't it?). I have to place code for raising event on specific page. For example home.seam, in some code reading data for page. If I'd like to log all the pages I have to do it with all pages? Or fx in code for common page template? But then I have to check if it's unique visit. Sorry if it's dumb question, but I've just started with Seam and actually had no time to go deep inside it.

                    • 7. Re: Visits counter
                      Leo van den berg Master

                      Just create a specific sessions-scoped bean or add it as a super function to the page-bean you want monitored (For instance if you want to know that some page has been seen, you could add it to a create method of the related bean. This will off-course only work if you reference the bean in the page.

                      • 8. Re: Visits counter
                        Francis Drake Newbie

                        thanks for help Leo, I try to implement it now

                        • 9. Re: Visits counter
                          Brandon Simpson Newbie

                          I haven't tried it...but if you created a page-scoped (or request-scoped) bean and annotated it with @Startup, wouldn't that cause it to be automatically created on every page without having to actually use it explicitly? Worth a shot!