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

 

开篇先简单说到这儿,欢迎拍砖。

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