随tomcat启动的Servlet程序
由于需要做一定定时轮询程序,自己写了一个Servlet小程序,在Servlet里面的Init函数中做一个Timer,定时执行程序。
代码如下:
public class MailStartup extends HttpServlet {
public static void main(String[] args) {
try {
new MailStartup().init();
} catch (ServletException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
super.init();
try{
System.out.println("待发送服务 SendMailOperator start time at.......................");
int length= Integer.parseInt(ServerConfig.getProperty("SendMailOperator.timer","30"));
Timer operatorTimer = new Timer();
SendMailOperator operator = new SendMailOperator();
operatorTimer.schedule(operator, 0, length);
}catch(Exception ex){
ex.printStackTrace();
}
}
还需要在web.xml做一些配置,就可以了,配置如下:
cn.efreight.email.MailStartup
重启tomcat即可。