Note: When the Job execution time exceeds the trigger interval, quartzjob
A common Job is implemented as follows:
Public class Job1: IJob {public void Execute (IJobExecutionContext context) {Console. writeLine (DateTime. now + ": Job1" + m) ;}} public class Program {static void Main (string [] args) {var props = new NameValueCollection (); // use the simple thread pool props ["quartz. threadPool. type "] =" Quartz. simpl. simpleThreadPool, Quartz "; // maximum number of threads props [" quartz. threadPool. threadCount "] =" 10 "; // thread priority: normal props [" quartz. threadPool. th ReadPriority "] =" Normal "; // initialize the scheduler IScheduler scheduler = new StdSchedulerFactory (props ). getScheduler (); // Cron trigger, which triggers ITrigger trig = TriggerBuilder every 1 second. create (). withCronSchedule ("0/1 ****? "). Build (); // Add job Job1 to scheduler in the scheduling plan. scheduleJob (JobBuilder. create <Job1> (). build (), trig); // start to execute scheduler. start (); Console. readLine (); scheduler. shutdown ();}}
The execution result is as follows: Job1 is executed exactly once every 1 second.
Now the question is: What will happen if the operation in Job1 takes a long time and exceeds the interval of 1 second? The Code is as follows:
Public class Job1: IJob {public void Execute (IJobExecutionContext context) {Console. writeLine (DateTime. now + ": Job1" + m); // wait for 5 seconds for Thread. sleep (5000 );}}
The execution result is as follows:
We will find that Quartz will still be triggered every 1 second according to our settings.
This is becauseBy default, when the Job execution time exceeds the interval, the scheduling framework immediately enables a new thread to execute the Job at our scheduled interval..
If we want to execute the next round of tasks after the current task is completed, that is, do not execute the tasks concurrently, what should we do?
Method 1: set the maximum number of threads for quartz. threadPool. threadCount to 1. In this way, when the second task is executed, if the task is not completed yet, the scheduler wants to open a new thread to execute the task, but we have set the maximum number of threads to one (I .e: if there are not enough threads available to the scheduler, the scheduler will wait until the current task is executed and then immediately schedule the next task. (Note: This method only applies to one Job in Quartz. If multiple jobs exist, execution of other jobs will be affected)
Method 2:Adding [DisallowConcurrentExecution] to the Job Class header indicates disabling concurrent execution. (This method is recommended)
// This Job cannot be concurrently executed (new threads are prohibited) [DisallowConcurrentExecution] public class Job1: IJob {}