Task scheduling refers to automatically performing tasks based on a given time point, a given time interval, or a given number of executions.
For example, we want a system to back up the database file every Sunday at 9 pm, and we can use task scheduling to implement it. For more convenience, we need to automatically start this scheduling after tomcat is started.
Here is the TimerTask API:
Below is the Timer class API
Two functions are implemented in the following example:
1. Monitor the startup and shutdown of tomcat's web container
2. When the web container is started, the task schedule allocates the task object, time and period.
In order to monitor changes in web containers, you need to register a listener in web.xml first
<?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 listener TimerListener as a custom class, see below --> <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>
The second step is to define a task object, the basic TimerTask
package com.util;import java.util.Timer;import java.util.TimerTask;/** * 1. Define a task object* JDK -- The task object needs to inherit TimerTask * */public class DataBackup extends TimerTask{ @Override public void run() { //Describe the task content that the current task needs to be executed System.out.println("Connect the database to back up table junctions and data to the local sql file"); }}Step 3. Monitor the changes in the web container. When the web container is started, allocate time and cycles for the 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 scce) { System.out.println("web container close"); timer.cancel(); } /** * Listen to web container startup*/ @Override public void contextInitialized(ServletContextEvent scet) { System.out.println("web container startup"); /** * Prepare Timer meter*/ timer = new Timer(); System.out.println("The timer is in preparation and starts allocating time and periods for task scheduling"); //Create task object DataBackup backup = new DataBackup(); //1 schedule(TimerTask task,Data time) //Schedule the specified task to start execution at the specified time//The month should be reduced by one //timer.schedule(backup, new Date(115,8,10,9,54,0)); //2 schedule(TimerTask task, Date firstTime, long period) //Schedule(backup,new Date(115,8,10,10,0,0) ,2000 ); //3schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //Schedule(TimerTask task, long delay) //timer.schedule(backup, 5000); //4 schedule(TimerTask task, long delay, long period) //timer.schedule(backup, 5000, 2000); //5. Depart once a task at 9 am every day //timer.schedule(backup, new Date(115,8,11,9,0,0) , 24*60*60*1000); //6. Depart once a task at 3 am every Saturday //timer.schedule(backup, new Date(115,8,12,3,0,0), 7*24*60*60*1000); }} In this way, when tomcat is started, the task object will start executing according to your allocated time and cycle.
Summary: Use Timer to implement task scheduling, first create a defined task object, such as Backup here; then instantiate a Timer, instantiate the task object, and call the corresponding method according to the requirements to allocate time and period to the task object. The above is the implementation of task scheduling. As for when this timer starts, that is, when the Timer starts instantiating, it needs to be determined based on the actual situation. For example, the above code is to start the timer after the web container is started.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.