This approach is purely Java code, which requires inheriting the TimerTask interface and overriding the Run method, which calls the Run method when the class is created.
The basic usage logic is: Put the business logic that you need to deal with in the class that inherits the TimerTask, then new a timer and call the schedule method, set a new class object in schedule as the parameter, and configure other time information, Examples are as follows:
Package Scheduletest;import Java.text.simpledateformat;import Java.util.date;import java.util.timer;import Java.util.timertask;import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;public Class ScheduleTest2 implements Servletcontextlistener {@Override public void contextdestroyed (Servletcontextevent a rg0) {} @Override/** * When starting project * @author: Tuzongxun * @Title: contextinitialized * @Descripti On:todo * @param @param arg0 * @date June 3 12:33:36 PM * @throws */public void Conte Xtinitialized (servletcontextevent arg0) {this.starttask (); }/** * Start method * * @author: Tuzongxun * @Title: StartTask * @param * @return void * @date June 3 12:34:23 PM * @throws * * public void StartTask () {Timer timer = new timer (); Timer.schedule (New Sche (), 0, 5000); }/** * Timer class * * @author tuzongxun123 * */CLass Sche extends TimerTask {@Override public void run () {Date date = new Date (); SimpleDateFormat sim = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String datestr = Sim.format (date); System.out.println ("This is the Java Timer 3, executed every five seconds, the current time:" + datestr); } }}
In the code aboveThe Sche class is the timed task class that you write, and I declare it in the form of an inner class. The Scheduled task is then invoked every five seconds in the StartTask method of the external class scheduleTest2.
Because this is just the case, the program does not trigger my class at startup, and the StartTask method cannot be called automatically, so the external class implements theServletcontextlistener Listener interface, while overriding contextinitialized this method, in order to be configured in Web. XML, so that the program can be invoked to run this scheduled task when it starts.
so the corresponding web. XML code changes to look like this:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>appversion </display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring2.xml</param-value> </context-param> <listener > <description>spring listener </description> <listener-class>scheduletest.scheduletest2 </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp </welcome-file> </welcome-file-list></web-app>
To illustrate the following, the spring2.xml here does not have a substantive effect in this example, only because the Web application requires an initial load file, which is the originalApplicationcontext.xml, so here is a file that has nothing to do with the beginning and the end, as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns:task= "Http://www.springframework.org/schema/task" xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http// www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp ://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd "></ Beans>
Java Timer Task (iii): TimerTask timed Tasks