How to Use Task Scheduling in java, java Use Task Scheduling
1. What is task scheduling?
It is used to implement the tasks that need to be executed regularly or repeatedly in the software development process (method | function ).
II. Implementation of Task Scheduling
- Database: trigger Stored Procedure
- Java:
1. JDK built-in Timer | TimerTask task object this blog details this method
2. quartz Sub-framework provided by spring (please refer to the next blog)
3. Use timer
Package com. etoak. util; import java. util. timer; import java. util. timerTask;/*** 1 define a task object * JDK-the task object must inherit TimerTask * 2 control the execution of the task object and its content through the Timer (time, cycle) * 3 when Timer is enabled-when the container is started * 4 when the web Container registers the listener-ServletContextListener */public class DataBackup extends TimerTask {private static int count = 0; private static Timer timer; // The constructor injects public DataBackup (Timer timer) {this. timer = timer;} // setter injects public void setTimer (Timer timer) {this. timer = timer;} @ Override public void run () {// describes the content of the task to be executed by the current task object System. out. println ("connecting to the database to back up the table structure and data to the local SQL file"); count ++; if (count = 10) timer. cancel ();}}
Package com. etoak. 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; @ Override public void contextDestroyed (ServletContextEvent arg0) {System. out. println ("web Container disabled"); timer. cancel () ;}@ Override public void contextInitialized (ServletContextEvent arg0) {System. out. println ("web Container started");/*** prepare Timer timer */Timer = new Timer (); System. out. println ("timer in preparation, start to allocate time for task content execution"); DataBackup backup = new DataBackup (timer); backup. setTimer (timer);/*** 1 schedule (TimerTask task, Date time) * executes the content pointed to by the task at the time specified * // timer. schedule (backup, new Date (115,8, 10 ,9, 39,0);/*** 2 schedule (TimerTask task, Date firstTime, long period) * starts execution at the specified time of firstTimer, execute the task at intervals of period milliseconds * // timer. schedule (backup, new Date (115,8, 2000,),);/*** 3 schedule (TimerTask task, long delay) * after the timer is started, task execution starts with delay in milliseconds * // timer. schedule (backup, 5000);/*** 4 schedule (TimerTask task, long delay, long period) * after the timer is started, the execution starts with a delay of delay in milliseconds, execute the task at intervals of period milliseconds * // timer. schedule (backup, 5000,200 0 ); /*** 1 trigger a task at every day [push message notification to the client] * 2 trigger a task at every Saturday [backup data] * 3 run every 1 minute one task, when the execution is completed 10 times, the execution is terminated * // 1 timer. schedule (backup, new Date (115,8, 11,9, 1000), 24*60*60 *); // 2 timer. schedule (backup, new Date (115,8, 1000,), 7*24*60*60 *); timer. schedule (backup, 0, 60*1000 );}}
Register the listener in 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"> <listener> <listener-class>com.etoak.util.TimerListener</listener-class> </listener> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.