1) Java. util. Timer
This method should be the most commonly used, but you need to manually start your task:
Timer timer = new timer ();
Timer. Schedule (New listbydaytimertask (), sums, 86400000 );
The listbydaytimertask class must use the run () method in extends timertask.
2) servletcontextlistener
This method is more convenient in the Web Container environment, so that after the Web server is started, you can
This task is automatically run without manual operations.
Set the listbydaylistener implements servletcontextlistener interface
Add the code to start the timer in the contextinitialized method.
Add the cancel timer code to the method, and then add the listener in Web. xml:
<Listener>
<Listener-class> com. QQ. Customer. listbydaylistener </listener-class>
</Listener>
3) org. springframework. Scheduling. Timer. scheduledtimertask
If you use spring, you do not need to write the Timer class.
Add the following content to. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "timer" class = "org. springframework. Scheduling. Timer. timerfactorybean">
<Property name = "scheduledtimertasks">
<List>
<Ref local = "mytimetask1"/>
</List>
</Property>
</Bean>
<Bean id = "mytimetask" class = "com. QQ. Timer. listbydaytimertask"/>
<Bean id = "mytimetask1" class = "org. springframework. Scheduling. Timer. scheduledtimertask">
<Property name = "timertask">
<Ref bean = "mytimetask"/>
</Property>
<Property name = "delay">
<Value> 10000 </value>
</Property>
<Property name = "Period">
<Value> 86400000 </value>
</Property>
</Bean>
</Beans>
4) the preceding three methods can be implemented in windows. If the program runs on a UNIX system, you can use a scheduled UNIX task to execute them:
Create a shell script to run the Java program that needs to be periodically executed in the script,
Java com. QQ. Timer. listbydaytimertask
Create a scheduled task and run the shell script: crontab-E. You can find the Unix information by using the syntax for creating the scheduled task.