学习网考试学习资料

Gzu521.com

定时执行任务的三种方法

JAVA认证   点击:次   发布时间:2007-1-31   【字体: 】   来源:Gzu521.com
贵 州 学 习 网
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:

com.qq.customer.listbydaylistener


3)org.springframework.scheduling.timer.scheduledtimertask
如果你用spring,那么你不需要写timer类了,在schedulingcontext-timer
.xml中加入下面的内容就可以了:



















10000


86400000


责任编辑:gzu521

IT认证分类
计算机软件水平考试
全国计算机等级考试
思科认证
微软认证
ORACLE/CIW认证
Linux认证
JAVA认证
其它认证
分类推荐信息
更多...
大类最新文章
更多...