天天看點

Quartz.Net使用體會(一)

前段時間幫國外的一個客戶做一個名叫 DiskMan.Net 的項目,裡面一個重要的功能就是定期調用執行工作任務,客戶要求使用Quartz.Net這個第三方工具來實作計劃任務。之前根本沒接觸過Quartz.Net,找到Quartz.Net的官網,下載下傳了它的示例檔案,依樣劃葫蘆做了一些小job,感覺很有成就感,嘿嘿,從今天開始,陸續把一些寫程式的過程中的經驗教訓記錄于此,備以後查閱。也希望有應用到這個工具的筒子們共同讨論學習。

開篇 如何開始

所謂計劃任務,即按制定的工作計劃執行工作任務,從這句話中可以看出,計劃任務實際上隻需要定義兩個東西:

一,工作計劃(Schedule),即什麼時間開始,什麼時間結束,重複次數,執行時間間隔等等。

二,工作任務(Job),即要執行什麼動作,做什麼事。

按這樣來了解,應該就很明白Quartz.Net的工作原理了。

下面給一個例子,實際上就是改寫自Quartz.Net官網例子的example1

第一步:先定義一個工作任務(Job),這個工作任務(Job)就是供後面要定義的工作計劃調用的。例子裡()中的中文為我翻譯的,大家别見笑

/// <summary>

 /// This is just a simple job that says "Hello" to the world.(這是一個“Hello World”例子)

 /// </summary>

 /// <author>易江城(Junix Yi)</author>

    /// <author>(Email:[email protected])</author>

///這裡要強調的是,Quartz.Net的Job必須繼承自Quartz.Net的一個接口類IJob

///這個接口類隻有一個方法,即:Execute()方法,這個方法就是具體要完成的動作都寫在這裡面,計劃任務每次調用這個Job來Execute這個方法完成任務。

    public class HelloJob : IJob

 {

  /// <summary> 

  ///constructor 

  /// </summary>

  public HelloJob()

  {

  }

  /// <summary>

  /// 定義接口方法

/// </summary>

  public virtual void  Execute(IJobExecutionContext context)

  {

   // Say Hello to the World and display the date/time

//(向這個美好的世界打個招呼吧:俺來也)

  Console.WriteLine(string.Format("Hello World! - {0}", System.DateTime.Now.ToString("r")));

  }

 }

第二步, 定義執行計劃(Schedule),程式将按這個計劃來調用上面的Job并執行。

    /// <summary>

    /// This Example will demonstrate how to start and shutdown the Quartz

    /// scheduler and how to schedule a job to run in Quartz.

///本例将示範如何啟動和停止Quartz 計劃任務,并且如何按計劃調用執行一個Job

    /// </summary>

    /// <author>易江城(Junix Yi)</author>

    /// <author>(Email:[email protected])</author>

    public class SimpleExample

    {

    public static void Main()

  {

Run()

}

        private void Run()

        {

            // First we must get a reference to a scheduler

//首先我們得到一個計劃器(scheduler)

            ISchedulerFactory sf = new StdSchedulerFactory();

            IScheduler sched = sf.GetScheduler();

            // computer a time that is on the next round minute

//定義計劃任務啟動時間

            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);

            // define the job and tie it to our HelloJob class

//引用上面建立的Job并建立一個任務執行個體

            IJobDetail job = JobBuilder.Create<HelloJob>()

                .WithIdentity("job1", "group1")

                .Build();

            // Trigger the job to run on the next round minute

//建立一個計劃觸發器,即這個計劃觸發器将管理執行計劃,并在定義的時間點觸發Job并執行。

            ITrigger trigger = TriggerBuilder.Create()

                .WithIdentity("trigger1", "group1")

                .StartAt(runTime)

                .Build();

            // Tell quartz to schedule the job using our trigger

//綁定這個計劃觸發器和工作任務

            sched.ScheduleJob(job, trigger);

            // Start up the scheduler (nothing can actually run until the

            // scheduler has been started)

//啟動計劃任務

            sched.Start();

            // wait long enough so that the scheduler as an opportunity to

            // run the job!

//等待一段時間,你會發現奇迹

            // wait 65 seconds to show jobs

//等待65秒

            Thread.Sleep(TimeSpan.FromSeconds(65));

            // shut down the scheduler

//停止計劃任務

            sched.Shutdown(true);

        }

    }

開篇先簡單說到這兒,歡迎拍磚。