Search code examples
javawebservices-clienthitcounter

Tracking webservice usage in Java


I am using a 3rd party (address validation) WebService in our application. The license to the WS includes only certain number of calls, exceeding which we pay more. I am trying to keep track of the usage within our application, so we can warn the users, before they exceed. Sort of like a hit counter for Web Services.

I am currently using a Static variable in the (controller) class to track it. This works, but only until the server gets restarted, at which time it resets to 0 again.

Is there a way to keep the counter running across restarts? I saw some suggestions about serializing static variable. Is this the right approach? Or should I read/write to a file/DB table every time I make the request (sounds costly).

MY webservice client will be running in an old Sybase EAServer (built around Apache Tomcat). So, I can only use Java 1.4.

Thanks for any comments or suggestions.


Solution

  • I would go: every WS call, update a counter on database.

    If you have many users accessing the WS Client and all of them writing to simple text file, you are going to have trouble on concurrent access to the file.

    If you try to use some in-memory architecture, and your app. crashes you are going to lose the count information.

    So use a database.