Write a timed task class, inherit Java.tuil.TimerTask, rewrite the Run () method, and implement the task business code in the method.
Package Com.gao.timertask;import Java.util.timertask;public Class Mytimertask extends TimerTask {public mytimertask () {} @Override public void Run () {///Timed Task Business logic Code}}
Write a Timer management class, Start () method to start the timer, customize the time interval for task execution, and join the timed task.
package com.gao.timertask;import java.util.calendar;import java.util.date;import java.util.timer;public class timermanager { /** * interval */ private static final to perform tasks long PERIOD_DAY = 24 * 60 * 60 * 1000; private int hour = 2; private int minute = 0; private int second = 0; private mytimertask timertask; public timermanager () {} public int gethour () { Return hour; } public void sethour (Int hour) { this.hour = hour; } public int getminute () { return minute; } public void setminute (Int minute) { this.minute = minute; } public int getsecond () { return second; } public void setsecond (Int second) { this.second = second; } public mytimertask gettimertask () { return timerTask; } public vOid settimertask (Mytimertask timertask) { this.timertask = timertask; } // increase or decrease the number of days public date addday (Date date, int num) { calendar startdt = calendar.getinstance (); startdt.settime (date); Startdt.add (Calendar.day_of_month, num); return Startdt.gettime (); } /** * Start Timer */ public void Start () { Calendar calendar = Calendar.getinstance (); &nbSp; calendar.set (Calendar.hour_of_day, hour); Calendar.set (Calendar.minute, minute); calendar.set ( Calendar.second, second); date date=calendar.gettime ( //The first time the scheduled task is executed //if the time of the first scheduled task is less than the current time //at the time of the first execution of the scheduled task plus one day so that the task executes at the next point in time. If one day is not added, the task executes immediately. if (Date.before (new date)) { date = this.addday (date, 1); } timer timer = new timer (); //schedules the specified task to begin repeating fixed deferred execution at a specified time. &nbSp; timer.schedule (timertask, date,period_day); }}
Use the listener to start the timer.
package com.gao.listener;import javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;import org.springframework.web.context.webapplicationcontext;import org.springframework.web.context.support.WebApplicationContextUtils;import com.gao.timertask.timermanager;public class mytimertasklistener implements servletcontextlistener { @Override Public void contextdestroyed (SERVLETCONTEXTEVENT SCE) { } @Override public void contextinitialized (SERVLETCONTEXTEVENT SCE) { WebApplicationContext context = WebApplicationContextUtils. getrequiredweBapplicationcontext (Sce.getservletcontext ()); timermanager timermanager = (Timermanager) context.getbean ("TimerManager"); timermanager.start ();//Start Timer manager }}
This article is from the "8731262" blog, please be sure to keep this source http://8741262.blog.51cto.com/8731262/1588884
Java EE Timers