Net作業調度(一) -Quartz.Net

來源:互聯網
上載者:User

標籤:winform   blog   http   io   ar   使用   for   sp   資料   

 背景

很多時候,項目需要在不同個時刻,執行一個或很多個不同的作業。

Windows執行計畫這時並不能很好的滿足需求了。 這時候需要一個更為強大,方便管理,支援叢集的作業調度了。

介紹

Quartz一個開源的作業調度架構,OpenSymphony的開源項目。Quartz.Net 是Quartz的C#移植版本。

它一些很好的特性:

1:支援叢集,作業分組,作業遠端管理。 

2:自訂精細的時間觸發器,使用簡單,作業和觸發分離。

3:資料庫支援,可以寄宿Windows服務,WebSite,winform等。

 

實戰

一:一些基礎概念。

   Scheduler     作業度器。

   IJob             作業介面。 繼承並實現Execute, 編寫執行的具體作業邏輯。

  JobBuilder       根據設定,產生一個詳細作業資訊(JobDetail)。

  TriggerBuilder   根據規則,生產對應的Trigger

二:Nuget安裝  PM> Install-Package Quartz 。

三:執行個體 (詳細注釋,不單獨介紹)。

  

 static void Main(string[] args)        {            //從工廠中擷取一個調度器執行個體化            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();            scheduler.Start();       //開啟調度器            //==========例子1(簡單使用)===========            IJobDetail job1 = JobBuilder.Create<HelloJob>()  //建立一個作業                .WithIdentity("作業名稱", "工作群組")                .Build();            ITrigger trigger1 = TriggerBuilder.Create()                                        .WithIdentity("觸發器名稱", "觸發器組")                                        .StartNow()                        //現在開始                                        .WithSimpleSchedule(x => x         //觸發時間,5秒一次。                                            .WithIntervalInSeconds(5)                                            .RepeatForever())              //不間斷重複執行                                        .Build();            scheduler.ScheduleJob(job1, trigger1);      //把作業,觸發器加入調度器。            //==========例子2 (執行時 作業資料傳遞,時間運算式使用)===========            IJobDetail job2= JobBuilder.Create<DumbJob>()                                        .WithIdentity("myJob", "group1")                                        .UsingJobData("jobSays", "Hello World!")                                        .Build();            ITrigger trigger2 = TriggerBuilder.Create()                                        .WithIdentity("mytrigger", "group1")                                        .StartNow()                                        .WithCronSchedule("/5 * * ? * *")    //時間運算式,5秒一次                                              .Build();            scheduler.ScheduleJob(job2, trigger2);                           //scheduler.Shutdown();         //關閉調度器。        }

  

 

 /// <summary>    /// 作業    /// </summary>    public class HelloJob : IJob    {        public void Execute(IJobExecutionContext context)        {            Console.WriteLine("作業執行!");        }    }

  

public class DumbJob : IJob    {        /// <summary>        ///  context 可以擷取當前Job的各種狀態。        /// </summary>        /// <param name="context"></param>        public void Execute(IJobExecutionContext context)        {            JobDataMap dataMap = context.JobDetail.JobDataMap;            string content = dataMap.GetString("jobSays");            Console.WriteLine("作業執行,jobSays:" + content);        }    } 

 

其他

WithCronSchedule("") 強大的時間運算式。

WithSimpleSchedule(x) 一般夠用了。

 

參考資源

Quartz.Net官方2.X教程  http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/index.html

Quartz.Net開源地址   https://github.com/quartznet/quartznet

 

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.