/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/StatisticsListener.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/StatisticsListener.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1022 - (show annotations) (download)
Mon Aug 30 15:49:20 2010 UTC (13 years, 8 months ago) by torben
File size: 1781 byte(s)
Avoid saving statistics on test servers
1 package dk.thoerup.traininfoservice;
2
3 import java.util.Timer;
4 import java.util.TimerTask;
5 import java.util.logging.Logger;
6
7 import javax.servlet.ServletContext;
8 import javax.servlet.ServletContextEvent;
9 import javax.servlet.ServletContextListener;
10 import javax.servlet.annotation.WebListener;
11
12 @WebListener
13 public class StatisticsListener implements ServletContextListener {
14
15 Logger log = Logger.getLogger(StatisticsListener.class.getName());
16
17 Timer timer = null;
18 int update_interval;
19
20 public void contextInitialized(ServletContextEvent sce) {
21
22 update_interval = getUpdateInterval( sce.getServletContext() );
23
24 log.info("StatisticsListener::contextInit called update_interval=" + update_interval);
25
26 if (! sce.getServletContext().getRealPath("/").startsWith("/home/app/") ) {
27 //if path not starts with /home/app then it is not running on a production server and we only collects stats on prod servers
28 return;
29 }
30
31 if ( update_interval > 0 ) {
32 timer = new Timer();
33
34 TimerTask statsUpdate = new TimerTask() {
35 @Override
36 public void run() {
37 Statistics.getInstance().saveStats();
38 }
39 };
40
41 timer.schedule(statsUpdate, 0, update_interval);
42 }
43 }
44
45
46 public void contextDestroyed(ServletContextEvent sce) {
47 log.info("StatisticsListener::contextDestroyed");
48 if ( update_interval > 0 ) {
49 timer.cancel();
50 Statistics.getInstance().saveStats();
51 }
52 }
53
54 private int getUpdateInterval(ServletContext cntx) {
55 int interval = 0;
56 try {
57 String intervalStr = cntx.getInitParameter("stats_interval");
58 interval = Integer.parseInt(intervalStr);
59 } catch (Exception e) {}
60
61 return interval;
62 }
63 }

  ViewVC Help
Powered by ViewVC 1.1.20