【Quartz.Net】.net 下使用Quartz.Net

來源:互聯網
上載者:User

標籤:creat   調度   stat   mon   代碼   特殊字元   tde   cin   某月   

Quartz.net是作業調度架構

1. 項目中添加quartz.net的引用(這裡使用nuget管理)

 

 

 

建立一個類TimingJob,該類主要用於實現任務邏輯

 
using Quartz;using System;namespace QuartzNetDemo{    /// <summary>    /// 定時任務類    /// </summary>    public class TimingJob : IJob    {        public void Execute(IJobExecutionContext context)        {            //將要定時執行的邏輯代碼寫於此處            Console.WriteLine("任務執行了");        }    }}

 

在Program.cs中:(這裡是控制台應用程式)

 
using Quartz;using Quartz.Impl;using System;namespace QuartzNetDemo{    class Program    {        static void Main(string[] args)        {            ISchedulerFactory sf = new StdSchedulerFactory();            IScheduler scheduler = sf.GetScheduler();            IJobDetail job = JobBuilder.Create<TimingJob>().WithIdentity("job1", "mygroup").Build();            ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(5)).WithCronSchedule("/2 * * ? * *").Build();            scheduler.ScheduleJob(job,trigger);            scheduler.Start();        }    }}
 

程式運行時,經過5秒後,每間隔2秒輸出資訊

 

 

Cron運算式:

quartz.NET中的cron運算式使用形如 "/5 * * ? * * *"  這樣的7位運算式,最後一位年非必選,運算式從左至右,依此是秒、分、時、月第幾天、月、周幾、年

名稱 範圍 可使用的特殊字元
Seconds 0-59 , - * /
Minutes 0-59 , - * /
Hours 0-23 , - * /
Day of month 1-31 , - * ? / L W
Month 1-12 or JAN-DEC , - * /
Day of week 1-7 or SUN-SAT , - * ? / L #
Year 空, 1970-2099 , - * /

 

特殊字元與解釋:

, 或的意思。
/a/b。 a:代表起始時間,b頻率時間。 
*頻率。 即每一次波動。
-區間。 
?任意值 。 即每一次波動。只能用在DayofMonth和DayofWeek,二者衝突。指定一個另一個一個要用?
L表示最後。 只能用在DayofMonth和DayofWeek
W工作日。 表示最後。 只能用在DayofWeek
#x#y。 只能用DayofMonth。 某月的第y個星期x

【Quartz.Net】.net 下使用Quartz.Net

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.