1. Using java.util.Timer
This method should be the most common, but this method needs to manually start your task
Timer timer=new timer (); Timer.schedule (new listbydaytimertask (), 10000,86400000);
The Listbydaytimertask class here must extends the run () method inside the TimerTask and then run the method inside 写你要执行的内容 .
2. Context listeners can be used in aweb container environment Servletcontextlistener
This method is convenient in a web container environment, so that the task can be run automatically after the Web server is started, without the need for manual action. Add the Listbydaylistener implements Servletcontextlistener interface to the Contextinitialized method by adding the code to start the timer in the contextdestroyed method to add the code to cancel the timer, and then in Web. XML, add Listener:
< listener>< listener-class>com.qq.customer.ListByDayListener</listener-class ></listener>
3. How to use the Spring framework:
If you use spring, then you do not need to write the timer class, add the following content in the config file Schedulingcontext-timer.xml:
<? xmlversion= "1.0"encoding= "UTF-8"?>< ! DOCTYPEbeans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd"><Beans><BeanID= "Timer"class= "Org.springframework.scheduling.timer.TimerFactoryBean">< Propertyname= "Scheduledtimertasks"><List><refLocal= "MyTimeTask1"/></list></property></bean><BeanID= "Mytimetask"class= "Com.qq.timer.ListByDayTimerTask"/><BeanID= "MyTimeTask1"class= "Org.springframework.scheduling.timer.ScheduledTimerTask">< Propertyname= "TimerTask"><refBean= "Mytimetask"/></property>< Propertyname= "Delay"><value>10000</value></property>< Propertyname= "Period"><value>86400000</value></property></bean></beans>
Three ways Java performs tasks on a timed basis