Where to go net internship summary: Development of timed tasks (Javaweb)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.