由于需要做一定定时轮询程序,自己写了一个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做一些配置,就可以了,配置如下:
<servlet>
<servlet-name>MailSend</servlet-name>
<servlet-class>
cn.efreight.email.MailStartup
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MailSend</servlet-name>
<url-pattern>/mail/*</url-pattern>
</servlet-mapping>
重启tomcat即可。