Originally to do the purpose of data mining in which network, the structure has become a system development ...
But still more serious to do for three months, the teacher is very agree with my work attitude and results ...
The internship is coming to an end, summarizing a few things that have not been noticed before to become habits and problems, to share to everyone.
At the same time to play an advertisement: to which network Internal audit department recruit Javaweb Development intern, Time is very free, a week, Saturday Sunday even can, the time of the small partners to give me a message ah, earn a pocket money, but also a long experience .... (GPA, want to work a senior dog is the most suitable ... )
In fact, the timing task is very simple, JS has actually done, is the timer class Timer.schedule (timertask task, Date time, long period) method just , three parameters are: task, Delay, interval.
Give a complete code:
The first is the Bugxmltimer class:
public class Bugxmltimer {public Timer timer;public void Timerstart () {timer = new timer ();D ate datetime=new Date ();D A Te midnightdate=new Date (); SimpleDateFormat sdf1 = new SimpleDateFormat ("Yyyy-mm-dd"); SimpleDateFormat sdf2 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); try { midnightdate = Sdf2.parse (Sdf1.format (datetime) + "10:06:00"), } catch (ParseException e) { //TODO Auto-generated Catch block e.printstacktrace (); } SYSTEM.OUT.PRINTLN ("before task"); Long time = Midnightdate.gettime ()-datetime.gettime (); Execute immediately, then execute once every 10s timer.schedule (New Bugxmltimertask (), 0, 10000);//time} public void Timerstop () {if ( Timer!=null) Timer.cancel ();} public static void Main (string[] args) {Bugxmltimer mytimer=new bugxmltimer (); TODO auto-generated Method Stub Mytimer.timerstart ();} }
Next is the TimerTask class:
<pre name= "code" class= "Java" >public class Bugxmltimertask extends TimerTask { @Override public void Run ( { System.out.print ("Run Task"); try {<strong>sendmail ();//The next blog teaches you to send mail </strong>} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Finally add the Listener class:
public class Mytimerlistener implements Servletcontextlistener {<span style= "White-space:pre" ></span> Private Bugxmltimer MyTimer = new Bugxmltimer (); public void contextinitialized (Servletcontextevent event) { mytimer.timerstart (); } public void contextdestroyed (Servletcontextevent event) { mytimer.timerstop (); } }
Finally, don't forget to configure the Listener node for Web. xml:
<listener> <listener-class>com. Timelistener.mytimerlistener</listener-class></listener>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Where to go net internship summary: Development of timed tasks (Javaweb)