1 java.util.timer
这个方法应该是最常用的,不过这个方法需要手工启动你的任务:
timer timer=new timer();
timer.schedule(new listbydaytimertask(),10000,86400000);
这里的listbydaytimertask类必须extends timertask里面的run()方法。
2 servletcontextlistener
这个方法在web容器环境比较方便,这样,在web server启动后就可以自动运行该任务,不需要手工操作。
将listbydaylistener implements servletcontextlistener接口,在contextinitialized方法中加入启动timer的代码,在contextdestroyed方法中加入cancel该timer的代码;然后在web.xml中,加入listener:
3 org.springframework.scheduling.timer.scheduledtimertask
如果你用spring,那么你不需要写timer类了,在schedulingcontext-timer.xml中加入下面的内容就可以了:
xml version="1.0" encoding="utf-8"?>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器