Task Scheduling-implemented using java. util. Timer, java. util. timer

Source: Internet
Author: User

Task Scheduling-implemented using java. util. Timer, java. util. timer

Task Scheduling refers to automatic task execution based on a given time interval or a specified number of executions.
For example, if we want a system to back up database files every Sunday at, we can use task scheduling. For better convenience, we need to automatically start this scheduling after tomcat is started.
The following is the TimerTask API:

Below is the Timer class API

The following example provides two functions:
1. Monitor the startup and shutdown of tomcat web containers
2. After the web container is started, the task scheduling allocates the task object, time, and cycle.

To monitor changes in web containers, you must register listeners in web. xml.

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <! -- Add the listener TimerListener as a custom class. For more information, see <listener> <listener-class> com. util. timerListener </listener-class> </listener> <display-name> </display-name> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>

Step 2: Define a task object, basic TimerTask

Package com. util; import java. util. timer; import java. util. timerTask;/*** 1. define a task object * JDK -- the task object must inherit TimerTask */public class DataBackup extends TimerTask {@ Override public void run () {// description of the content of the task to be executed by the current task: System. out. println ("connecting to the database to back up table knot and data to a local SQL file ");}}

Step 3. Monitor the changes of web containers. After a web container is started, allocate the time and cycle for task scheduling. The following Code contains several examples.

Package com. util; import java. util. date; import java. util. timer; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; public class TimerListener implements ServletContextListener {private static Timer timer;/*** listen to web Container close */@ Override public void contextDestroyed (ServletContextEvent sce) {System. out. println ("web Container disabled"); timer. cancel ();}/*** listen to web Container startup */@ Override p Ublic void contextInitialized (ServletContextEvent sce) {System. out. println ("web Container startup");/*** prepare the Timer timer */Timer = new Timer (); System. out. println ("timer in preparation, start to allocate time and cycle for task scheduling"); // create the task object DataBackup backup = new DataBackup (); // 1 schedule (TimerTask task, data time) // schedule the execution of the specified task to start at the specified time // minus one in the month // timer. schedule (backup, new Date (115,8, 10, 9, 54,0); // 2 schedule (TimerTask task, Date firstTime, long period) // Schedule the specified task to execute at a fixed delay at a specified time. // timer. schedule (backup, new Date (115,8, 10, 2000),); // 3 schedule (TimerTask task, long delay) // schedule the task to be executed after the specified delay. // Timer. schedule (backup, 5000); // 4 schedule (TimerTask task, long delay, long period) // timer. schedule (backup, 5000,200 0); // 5. start a task at every morning // timer. schedule (backup, new Date (115,8, 11,9, 1000), 24*60*60 *); // 6. start a task at every Saturday. // timer. schedule (backup, new Date (115,8, 1000,), 7*24*60*60 );}}

In this way, after tomcat is started, the task object will be executed according to the time and cycle you have allocated.
Conclusion: To use Timer for task scheduling, first create a task object, for example, Backup here; then instantiate a Timer and instantiate the task object, assign the time and period for the task object according to the corresponding call method as required. The preceding figure shows the implementation of task scheduling. when to start the Timer, that is, when to instantiate the Timer, the Timer should be determined based on the actual situation, for example, the above Code starts the timer when the web Container starts.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.