The first method is common and easy to use.
1. In application development, periodic operations are often required, such as performing an operation every 5 minutes.
. The most convenient and efficient way to implement such operations is to use the java. util. Timer tool class.
Private java. util. Timer timer;
Timer
= New timer (true );
Timer. Schedule (
New java. util. timertask (){
Public void run () {// server. checknewmail (); Method to operate }}, 0, 5*60*1000 );
The
One parameter is the method to be operated, the second parameter is to set the delay time, and the third parameter is the period setting. The operation is executed at intervals.
After using these lines of code, Timer itself will call the server. checknewmail () method every five minutes without starting the thread. Timer itself is also a multiline
For synchronization, multiple threads can share one timer without external synchronization code.
2,
(1) timer. Schedule (timertask
Task, date time) scheduled to execute the specified task at the specified time.
(2) timer. Schedule (timertask task, date
Firsttime, long period) Schedule the specified task to execute at the specified time.
(3) timer. Schedule (timertask
Task, long delay) scheduled to execute the specified task after the specified delay.
(4) timer. Schedule (timertask
Task, long delay, long period) Schedule the specified task to execute the same fixed delay from the specified delay.
(5) timer. scheduleatfixedrate (timertask
Task, date firsttime, long period) Schedule the specified task to execute at a fixed rate at a specified time.
(6) timer. scheduleatfixedrate (timertask
Task, long delay, long period) Schedule the specified task to start repeating at a fixed rate after the specified delay. for example, Timer time = new timer (true); // version information reporting Timer
Time
. Schedule (New uploadvervsiondataimpl (CTX), 1000*60 *); // report once every 30 minutes Description: Execute once every 30 minutes
Uploadvervsiondataimpl (CTX)
Public class uploadvervsiondataimpl extends timertask {
Public static webapplicationcontext CTX;
Public uploadvervsiondataimpl (webapplicationcontext CTX ){
This. CTX = CTX;
}
@ Override
Public void run () {// 1 get version information; 2 Report to the center (WebService)
// Method to be executed
} Second method Scheduler
First add the required class package: quartz-1.5.2.jar, quartz-all-1.5.2.jar, quartz-JBoss-
1.5.2.jar
// Set a scheduled job
Public
Void startscheduler (httpservletrequest requests) throws
Schedulerexception {
Try {
// Create a Job scheduler
Scheduler
Schedult= stdschedulerfactory. getdefaschscheduler ();
// Determine whether a job exists in the Job Scheduling. If yes, delete it.
If (! Schedshut. isshutdown ()){
Schedmed. deletejob ("receiveontimed", schedmed. default_group );
}
// Delete the scheduler job and create a new one
For the job to be completed, the class required for this job is receivejobd. Class, the job name is receiveontimes, and the group is
Scheduler. default_group
Jobdetail = new jobdetail ("receiveontimed ",
Scheduler. default_group,
Receivejobd. Class );
Int M = IPs. length;
Int n = tablenames. length;
// Set the parameters to be passed in to the job
Jobdetail. getjobdatamap (). Put ("scheduler ",
Scheduler );
Jobdetail. getjobdatamap (). Put ("iplength", M );
Jobdetail. getjobdatamap (). Put ("tablenamelength", N );
Jobdetail. getjobdatamap (). Put ("wcx", WC );
For (INT I
= 0; I <m; I ++ ){
Jobdetail. getjobdatamap (). Put ("ip" + I, IPS [I]);
For
(Int J = 0; j <n; j ++ ){
Jobdetail. getjobdatamap (). Put ("tablename" + J, tablenames [J]);
}
}
//
Create a trigger to determine when the job is triggered. The parameter is the trigger name, trigger Group, job name, job group, and time setting.
Trigger trigger = new
Crontrigger ("receiverontimetrigadh ",
Scheduler. default_group,
"Receiveontimed ",
Scheduler. default_group, time );
// Add the job and trigger to the Scheduler
Scheduler. schedulejob (jobdetail,
Trigger );
//
Start scheduling according to Settings
Scheduler. Start ();
} Catch (exception
E ){
E. printstacktrace ();
}
}
Processing of specific job classes:
Package
Com. bodhiya. schsystem. business;
Import org. Quartz. job;
Import
Org. Quartz. jobdatamap;
Import org. Quartz. jobexecutioncontext;
Import
Org. Quartz. jobexecutionexception;
Import org. Quartz. scheduler;
Import
Org. springframework. Web. Context. webapplicationcontext;
Public class receivejobd implements job {
Public void
Receivejobd (){
}
// The specific implementation of the job
Function
Public void execute (jobexecutioncontext
Context) throws
Jobexecutionexception {
Try {
// Create a jobdatamap object,
Used to receive transmitted Parameters
Jobdatamap
Datamap = context. getmergedjobdatamap ();
Int
Iplength = datamap. getint ("iplength ");
Int
Tablenamelength = datamap. getint ("tablenamelength ");
Webapplicationcontext
WC = (webapplicationcontext) datamap. Get ("wcx ");
String IP = "";
String tablename = "";
For (INT I = 0; I <iplength; I ++ ){
IP =
Datamap. getstring ("ip" + I );
For (Int J = 0; j <
Tablenamelength; j ++ ){
Tablename =
Datamap. getstring ("tablename" + J );
Exchangeaccess Ea = new exchangeaccess (IP, tablename, WC );
// Start the thread
EA. Start ();
}
}
Scheduler scheduler = (Scheduler)
Datamap. Get ("scheduler ");
Schedmed. deletejob ("receiveontimed", schedmed. default_group );
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}