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.ServletContextEvent; |
8 |
import javax.servlet.ServletContextListener; |
9 |
import javax.servlet.annotation.WebListener; |
10 |
|
11 |
@WebListener |
12 |
public class StatisticsListener implements ServletContextListener { |
13 |
|
14 |
Logger log = Logger.getLogger(StatisticsListener.class.getName()); |
15 |
|
16 |
Timer timer = null; |
17 |
int update_interval; |
18 |
|
19 |
public void contextInitialized(ServletContextEvent sce) { |
20 |
|
21 |
TraininfoSettings settings = SettingsListener.loadSettings(sce.getServletContext()); |
22 |
|
23 |
update_interval = settings.getStatsInterval(); |
24 |
|
25 |
if (! sce.getServletContext().getRealPath("/").startsWith("/home/app/") ) { |
26 |
//if path not starts with /home/app then it is not running on a production server and we only collects stats on prod servers |
27 |
log.info("This is not a prod. server, dont start Statistics saver"); |
28 |
return; |
29 |
} |
30 |
|
31 |
|
32 |
|
33 |
if ( update_interval > 0 ) { |
34 |
log.info("Statistics saver started with update_interval=" + update_interval); |
35 |
timer = new Timer(); |
36 |
|
37 |
TimerTask statsUpdate = new TimerTask() { |
38 |
@Override |
39 |
public void run() { |
40 |
Statistics.getInstance().saveStats(); |
41 |
} |
42 |
}; |
43 |
|
44 |
timer.schedule(statsUpdate, 0, update_interval); |
45 |
} else { |
46 |
log.info("Statistics saver disabled (update_interval == 0) "); |
47 |
} |
48 |
} |
49 |
|
50 |
|
51 |
public void contextDestroyed(ServletContextEvent sce) { |
52 |
log.info("StatisticsListener::contextDestroyed"); |
53 |
if ( timer != null ) { |
54 |
timer.cancel(); |
55 |
Statistics.getInstance().saveStats(); |
56 |
} |
57 |
} |
58 |
|
59 |
} |