com.watchingdogs.RefundsDogs:
package com.watchingdogs; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class RefundsDogs implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("Start Timer Task"); TimerTask myTimeTask = new MyTimeTask(); Timer timer = new Timer(); timer.schedule(myTimeTask, 1000, 1000 /* 1 Million Seconds*/ ); } @Override public void contextDestroyed(ServletContextEvent sce) { } class MyTimeTask extends TimerTask { @Override public void run() { System.out.println("This is dog."); } } }
之後將 com.watchingdogs.RefundsDogs 寫到 web.xml 的 listen-class 就行了:
<listener>
<listener-class>
com.watchingdogs.RefundsDogs
</listener-class>
</listener>
Reference:
http://stackoverflow.com/questions/2248971/running-periodic-task-at-server-side-for-servlet-jsp-mvc-website
沒有留言:
張貼留言