servlet定时任务

public void init() throws ServletException {
final ServletContext sc = this.getServletContext();
ApplicationContext ac = WebApplicationContextUtils
.getWebApplicationContext(sc);
smsdao = (SmstDao) ac.getBean("smsdao");

        String smsTemp = this.getInitParameter("smsTemp");//是否扫描
if("1".equals(smsTemp)){
Timer smstempTimer = new Timer(true);
smstempTimer.schedule(new TimerTask(){
public void run(){
try{
List<String> smsList = new ArrayList<String>();
String ids = "";
//查询
smsList = smsdao.scanSmsTemp();
//插入
smsdao.sendSmsTemp(ids);
smsdao.updateSmsTemp(ids);

}catch(Exception e){
e.printStackTrace();
}
}
},0, 1000 * 30);
}
}

======================================
web.xml
=====================================
<servlet>
    <servlet-name>SmsSender</servlet-name>
    <servlet-class>com.besys.platform.servlet.SmsSender</servlet-class>
    <init-param>
      <param-name>smsTemp</param-name>
      <param-value>1</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

猜你喜欢

转载自it-passerby.iteye.com/blog/2293695