I use the SPRINGMVC, the general talk about their own ideas bar:
To the page traffic statistics is the PV, the main interception, write an interceptor class to implement the Handlerinterceptor interface, intercept all requests, but it should be noted that the time to configure the Interceptor
Need to exclude the request that does not intercept, because for static resources Css,js, as well as the picture, but also has the backstage management system we do not need to intercept, we only intercepts the user to visit the page, namely you show
To the user of those pages configured to intercept probably this configuration
<mvc:interceptor>
<!--intercept path-->
<mvc:mapping path= "/**"/>
<mvc:exclude-mapping Path= "Admin URL is not disclosed"/><!--do not intercept background management-->
<mvc:exclude-mapping path= "/css/**"/> <mvc
: Exclude-mapping path= "/js/**"/>
<mvc:exclude-mapping path= "/images/**"/> <mvc
: Exclude-mapping path= "/fonts/**"/> <bean class= "com.colin.interceptor.InterceptAllRequest"/>
</mvc:interceptor>
Then intercept what the request needs to do. Remove the amount of traffic from the database. Then add 1. This is obviously unreasonable because there are too many requests, and frequent interactions with the database should be avoided,
I want to write a listener, Servletcontextlistener, listen to the initial ServletContext (only when the server is started to be created), server startup to the database to fetch traffic,
To the ServletContext (global domain, which can be taken out in any other request), and then each time a request adds 1 to the amount of traffic placed in the ServletContext (this is what is done in the Interceptor), When initializing the ServletContext also need to do one thing, opens the thread time to store in the ServletContext the amount of traffic in the database, how to execute regularly. I want to write a loop, run a loop and let the thread hibernate
2 hours (custom: Shorter time is less likely to cause loss of access data), then accidentally saw a JDK document a class, timer, a timer, detailed reference documents, it can be accessed to achieve the task of timing, the principle of the estimate is similar, this is probably the implementation process, the specific code will not be posted. A more serious problem is not taken into account: Since ServletContext is in memory
Only one object, concurrent access can cause thread-safety issues, preliminary thinking in ServletContext when a thread-safe collection, data access is placed in the collection