Four Methods for executing scheduled tasks in Java

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.