Note: When the Job execution time exceeds the trigger interval, quartzjob

Source: Internet
Author: User

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 {}

 

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.