2.2)web监听方式
示例代码:
package org.lzstone.action;
import javax.ejb.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class LzstoneMainListener implements ServletContextListener{
private Timer timer = null;
//初始化监听器,创建实例,执行任务
public void contextInitialized(ServletContextEvent event){
try {
LzstoneMain.run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//销毁监听器,停止执行任务
public void contextDestroyed(ServletContextEvent event){
try {
LzstoneMain.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
web.xml配置
<listener>
<listener-class>
org.lzstone.action.LzstoneMainListener
</listener-class>
</listener>