Use of Java Timers

Source: Internet
Author: User

Internship in Beijing this is also one months, also touched a lot of things, today's leadership meeting, said the server pressure is too big, the customer is not satisfied with the speed of access requirements are very harsh, some of our project operations, there is no need to write dynamic are changed to write static, Update the contents of the static HTML page regularly every day (because the customer updates some content on a regular basis and updates it only once a day).

Listen to me very much, I heard the first time Java timer operation! Deadly things, good pits Ah, the evening to spare time, learning a bit of Java timer, that is, some basic operation.

Java timers are used mainly in two classes:timer(scheduled task scheduling Class) and timertask(Specific task Class)

first introduce the TimerTask class bar , here we do not go directly to use this class, this class is an abstract class, we want to set our own specific content of the scheduled task, we can customize a mytimertask (name can be customized ha) class, But you have to inherit the TimerTask class, so that after inheriting, it will default overload a run method, and this method is our detailed planning task method, we can directly put the action we want to do, written in this method, this is in the most direct way, if you want to do a lot of tasks, You can also write other methods or classes, and we'll call them in the Run method.

Then is the timer class , hehe, directly with Bai, for the use of this class or refer to the following code I give it! Here I introduce some basic information about this class:

How to construct a timer class :

There seems to be only one,the Timer ()//construction method does not have to explain more, instantiate the purpose of the

Common methods:

Method Summary
  cancel()
Terminates this timer, discarding all currently scheduled tasks.

purge()
Removes all canceled tasks from this timer's task queue.

schedule(TimerTask task,Date time)
Schedules the specified task to execute at the specified time.

schedule(TimerTask task,Date firstTime, long period)
Schedules the specified task to begin repeating fixed deferred execution at a specified time.

schedule(TimerTask task, long delay)
Schedules the specified task to execute after a specified delay.

schedule(TimerTask task, long delay, long period)
Schedules the specified task to start repeating fixed deferred execution from the specified delay.

scheduleAtFixedRate(TimerTask task,Date firstTime, long period)
Schedules the specified task to start at a fixed rate of repetition at a specified time.

scheduleAtFixedRate(TimerTask task, long delay, long period)

Schedules the specified task to begin repeating a fixed rate of execution after a specified delay.

Here, I briefly describe the trigger and use of the timer:

Use, look at the following code, combined with the above, should be very clear! Little brother, if found wrong hope corrected ha!

public class Mytimertask extends TimerTask {private static int i = 0; @Overridepublic void Run () {//TODO auto-generated Me Thod stubSystem.out.println ("--------------" + (i++) + "---------------");}}

public class MyTimer {public static void main (String args[]) throws ParseException {Timer time = new timer (); SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");D ate Date = Format.parse ("2014-08-19 21:56:03"); Time.scheduleatfixedrate (New Mytimertask (), date, 1000);}}
There is a certain Java Foundation can be seen, I just in a simple Java class inside to trigger this task!

So how do we get our Java Web project to trigger this opportunity task when it is started, the server just opens, the scheduled task exists, how to do it, here I introduce the use of servlet to trigger:

The first thing we need to do is rewrite the above MyTimer class into a regular servlet class, as follows:

public class MyTimer extends HttpServlet {public void init () {Timer time = new Timer (); SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");D ate date;try {Date = Format.parse ("2014-08-19 23:00:03 "); Time.scheduleatfixedrate (new Mytimertask (), 0, 1000);} catch (ParseException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Then we need to configure the configuration file of our Project Web. xml file, we are in here, let the timer in the server opened when the load comes in, detailed configuration is as follows:

<servlet>     <servlet-name>Timer</servlet-name>     <servlet-class>action. timerutils.mytimer</servlet-class>     <load-on-startup>0</load-on-startup>     </servlet >
Let's briefly introduce the role of Load-on-startup in this. Also I see the netizen summarizes this, I did not know before!

Load-on-startup values are generally positive or 0, we usually take 0,1,2,3,4,5, the smaller the value, the higher the priority load

OK, wait for these days in the project to implement the specific implementation of the regular update static HTML content, in the see what needs to add something, in addition, it's late! Say good night to yourself, come on!






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.