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);
        }
    }

 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章