1)java.util.TimerYuYLinux联盟
这个方法应该是最常用的,不过这个方法需要手工启动你的任务:YuYLinux联盟
Timer timer=new Timer();YuYLinux联盟
timer.schedule(new ListByDayTimerTask(),10000,86400000);YuYLinux联盟
这里的ListByDayTimerTask类必须extends TimerTask里面的run()方法。
2)ServletContextListenerYuYLinux联盟
这个方法在web容器环境比较方便,这样,在web server启动后就可以YuYLinux联盟
自动运行该任务,不需要手工操作。YuYLinux联盟
将ListByDayListener implements ServletContextListener接口,在YuYLinux联盟
contextInitialized方法中加入启动Timer的代码,在contextDestroyedYuYLinux联盟
方法中加入cancel该Timer的代码;然后在web.xml中,加入listener:YuYLinux联盟
<listener>YuYLinux联盟
<listener-class>com.qq.customer.ListByDayListener</listener-class>YuYLinux联盟
</listener>
3)org.springframework.scheduling.timer.ScheduledTimerTaskYuYLinux联盟
如果你用spring,那么你不需要写Timer类了,在schedulingContext-timerYuYLinux联盟
.xml中加入下面的内容就可以了:YuYLinux联盟
<?xml version="1.0" encoding="UTF-8"?>YuYLinux联盟
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>YuYLinux联盟
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">YuYLinux联盟
<property name="scheduledTimerTasks">YuYLinux联盟
<list>YuYLinux联盟
<ref local="MyTimeTask1"/>YuYLinux联盟
</list>YuYLinux联盟
</property>YuYLinux联盟
</bean>
<bean id="MyTimeTask" class="com.qq.timer.ListByDayTimerTask"/>
<bean id="MyTimeTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">YuYLinux联盟
<property name="timerTask">YuYLinux联盟
<ref bean="MyTimeTask"/>YuYLinux联盟
</property>YuYLinux联盟
<property name="delay">YuYLinux联盟
<value>10000</value>YuYLinux联盟
</property>YuYLinux联盟
<property name="period">YuYLinux联盟
<value>86400000</value>YuYLinux联盟
</property>YuYLinux联盟
</bean>YuYLinux联盟
</beans>