实现ServletContextListener 接口,并覆盖其中的两个方法:
public class StartMonitorMain implements ServletContextListener {
public void contextInitialized(ServletContextEvent arg0) {
try {
while (true) {
Thread monitor = new MonitorThread();
monitor.start();
while (true) {
if (!monitor.isAlive()) {
break;
}
}
Thread.sleep(3 * 60 * 1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
}
在web.xml 中添加侦听器:
<listener>
<listener-class>cn.golaxy.ws.main.StartMonitorMain</listener-class>
</listener>