>>> Wai Mun <[EMAIL PROTECTED]> 02-Jun-01 3:26:00 PM >>>

>i'm thinking of writing every count for every servlet
>visit to a counter file. this file would record the datetime,
>name of servlet visited, and this file will be read easily
>(txt format) by means from browser or whatsoever.
>question is: how do i update the counter file efficiently?
>and also considering concurrent access. i thought of one:
>that is to open & close the file everytime the servlet is
>called. is this a good method, a preferred one? or anyone
>can suggest a better solution to this?

File i/o can be costly. Opening and closing files can be even more
costly.

I would design a log class that has a method:

  void log()

The log method would be used by your servlets, eg:

  Logger logger=null;

  public void init()
  {
     logger=new Logger(getServletName());
  }

   public void doGet(...
   {
       ...
       logger.log();
    }

The design of the log class is the clever part:

The constructor:
- opens a file for writing
- creates a seperate thread for managing the file

The log method:
- gets the current time (System.currentTimeMillis)
- enters the current time into an array of 1000 longs
- when the long array is full passes it to the file management thread
and creates a new array

The file management thread
- when it recieves the long array it writes out a line for each long,
converting the long to a date first and adding the servlet name
- every now and then closes the file and opens another one (eg:
daily)


I have a GPLed class called TimedLogWriter which can do the part of
swapping log files on a daily basis. If you want to see that you
should go to:

  http://savannah.gnu.org/cgi-bin/
      viewcvs/paperclips/paperclips/source/
      gnu/paperclips/utils/TimedLogWriter.java
      ?rev=1.2&content-type=text/vnd.viewcvs-markup

(note that is one whole URL but I've broken it into lines to make
things difficult for you /8-)

The TimedLogWriter can also write a logstamp on every log message,
but that's not what you want given the above scenario.

If you write such a class it would be great if you licenced it uthe
LGPL and contribed it to the Paperclips project.


Note that a variation on the scheme I outline above is to simply
count accesses for a particular resource and then write the count out
somewhere... possibly a database. That is an extreemly efficient way
of doing counting, much more efficient even than the scheme above (but
the scheme above has the advantage of time stamps).


Nic Ferrier

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to