In most items, you need to complete some specific tasks at a specific time or frequency. In this article we will demonstrate how to implement it with a standard Java Timer API.
Most commercial applications use statements and statistics, it is difficult to imagine that there is no data to help us analyze future trends, we still have to do what is the system. The problem is so much data how we should trigger, if the processing is not much affected by the system performance. A preferred way is to avoid the peak application, let the server complete these things when you are free.
Here is my program:
Package net.nighttale.scheduling;
/ **
* @Author kevin zhou
*
* Implement Job Scheduling in Java
* Create date 2004-7-13
* /
Import java.util.timertask;
Import java.util.calendar;
Import java.util.date;
Import java.util.timer;
Public class reportGenerator Extends Timertask {
/ * (Non-javadoc)
* @see java.util.timertask # run ()
* /
Public void run () {
System.out.println ("generating report");
}
}
Class mainApplication {
Public static void main (String [] args) {
Timer Timer = New Timer ();
Calendar Date = Calendar.getInstance ();
Date.set
Calendar.day_of_week,
Calendar.sunday
);
Date.Set (Calendar. Hour, 0);
Date.set (Calendar.minute, 0);
Date.set (Calendar.second, 0);
Date.set (Calendar.MilliseCond, 0);
// Schedule to Run Every Sunday in Midnight
Timer.schedule
New reportGenerator (),
Date.gettime (),
1000 * 60 * 60 * 24 * 7
);
}
}
I briefly explain it, in our example, reportgenerator inherits java.util.timertask, inheriting java.lang.Runnable, and we need to override the Run () method.
When calling, we use the schedule () method to perform it at 0:06 every Sunday, avoid the peak of the server, and achieve the purpose of Job Scheduling.