正如前面所提到的那样,通过实现IJob接口来使你的.NET组件可以很简单地被scheduler执行。下面是IJob接口:
namespace Quartz

{

     /**//// <summary> 

     /// The interface to be implemented by classes which represent a 'job' to be

     /// performed.

     /// </summary>

     /// <remarks>

     /// Instances of this interface must have a <see langword="public" />

     /// no-argument constructor. <see cref="JobDataMap" /> provides a mechanism for 'instance member data'

     /// that may be required by some implementations of this interface.

    /// </remarks>

     /// <seealso cref="JobDetail" />

     /// <seealso cref="IStatefulJob" />

     /// <seealso cref="Trigger" />

     /// <seealso cref="IScheduler" />

     /// <author>James House</author>

     /// <author>Marko Lahma (.NET)</author>

     public interface IJob

     {

         /**//// <summary>

         /// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />

         /// fires that is associated with the <see cref="IJob" />.

        /// </summary>

         /// <remarks>

         /// The implementation may wish to set a  result object on the 

         /// JobExecutionContext before this method exits.  The result itself

         /// is meaningless to Quartz, but may be informative to 

         /// <see cref="IJobListener" />s or 

         /// <see cref="ITriggerListener" />s that are watching the job's 

         /// execution.

         /// </remarks>

         /// <param name="context">The execution context.</param>

         void Execute(JobExecutionContext context);

     }

}
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
            ILog log = LogManager.GetLogger(typeof (SimpleTriggerExample));

            log.Info("------- Initializing -------------------");

            // First we must get a reference to a scheduler
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();

            log.Info("------- Initialization Complete --------");

            log.Info("------- Scheduling Jobs ----------------");

            // jobs can be scheduled before sched.start() has been called

            // get a "nice round" time a few seconds in the future
            DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15);

            // job1 will only fire once at date/time "ts"
            JobDetail job = new JobDetail("job1", "group1", typeof (SimpleJob));
            SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", ts);

            // schedule it to run!
            DateTime ft = sched.ScheduleJob(job, trigger);
            log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            // job2 will only fire once at date/time "ts"
            job = new JobDetail("job2", "group1", typeof (SimpleJob));
            trigger = new SimpleTrigger("trigger2", "group1", "job2", "group1", ts, null, 0, 0);
            ft = sched.ScheduleJob((job), trigger);
            log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            // job3 will run 11 times (run once and repeat 10 more times)
            // job3 will repeat every 10 seconds (10000 ms)
            job = new JobDetail("job3", "group1", typeof (SimpleJob));
            trigger = new SimpleTrigger("trigger3", "group1", "job3", "group1", ts, null, 10, 10000L);
            ft = sched.ScheduleJob(job, trigger);
            log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            // the same job (job3) will be scheduled by a another trigger
            // this time will only run every 70 seocnds (70000 ms)
            trigger = new SimpleTrigger("trigger3", "group2", "job3", "group1", ts, null, 2, 70000L);
            ft = sched.ScheduleJob(trigger);
            log.Info(string.Format("{0} will [also] run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            // job4 will run 6 times (run once and repeat 5 more times)
            // job4 will repeat every 10 seconds (10000 ms)
            job = new JobDetail("job4", "group1", typeof (SimpleJob));
            trigger = new SimpleTrigger("trigger4", "group1", "job4", "group1", ts, null, 5, 10000L);
            ft = sched.ScheduleJob(job, trigger);
            log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            // job5 will run once, five minutes past "ts" (300 seconds past "ts")
            job = new JobDetail("job5", "group1", typeof (SimpleJob));
            trigger = new SimpleTrigger("trigger5", "group1", "job5", "group1", ts.AddMilliseconds(300*1000), null, 0, 0);
            ft = sched.ScheduleJob(job, trigger);
            log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            // job6 will run indefinitely, every 50 seconds
            job = new JobDetail("job6", "group1", typeof (SimpleJob));
            trigger =
                new SimpleTrigger("trigger6", "group1", "job6", "group1", ts, null, SimpleTrigger.REPEAT_INDEFINITELY,
                                  50000L);
            ft = sched.ScheduleJob(job, trigger);
            log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            log.Info("------- Starting Scheduler ----------------");

            // All of the jobs have been added to the scheduler, but none of the jobs
            // will run until the scheduler has been started
            sched.Start();

            log.Info("------- Started Scheduler -----------------");

            // jobs can also be scheduled after start() has been called
            // job7 will repeat 20 times, repeat every five minutes
            job = new JobDetail("job7", "group1", typeof (SimpleJob));
            trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", ts, null, 20, 300000);
            ft = sched.ScheduleJob(job, trigger);
            log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 
                job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval/1000)));

            // jobs can be fired directly (rather than waiting for a trigger)
            job = new JobDetail("job8", "group1", typeof (SimpleJob));
            job.Durable = (true);
            sched.AddJob(job, true);
            log.Info("'Manually' triggering job8");
            sched.TriggerJob("job8", "group1");

            log.Info("------- Waiting 30 seconds --------------");

            try
            {
                // wait 30 seconds to show jobs
                Thread.Sleep(30*1000);
                // executing
            }
            catch (ThreadInterruptedException)
            {
            }

            // jobs can be re-scheduled  
            // job 7 will run immediately and repeat 10 times for every second
            log.Info("------- Rescheduling --------------------");
            trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", DateTime.Now, null, 10, 1000L);
            NullableDateTime ft2 = sched.RescheduleJob("trigger7", "group1", trigger);
            if (ft2.HasValue)
            {
                log.Info("job7 rescheduled to run at: " + ft2.Value.ToString("r"));
            }
            else
            {
                log.Error("Reschedule failed, date was null");
            }

            log.Info("------- Waiting five minutes ------------");
            try
            {
                // wait five minutes to show jobs
                Thread.Sleep(2*1000);
                // executing
            }
            catch (ThreadInterruptedException)
            {
            }

            log.Info("------- Shutting Down ---------------------");

            sched.Shutdown(true);

            log.Info("------- Shutdown Complete -----------------");

            // display some stats about the schedule that just ran
            SchedulerMetaData metaData = sched.GetMetaData();
            log.Info(string.Format("Executed {0} jobs.", metaData.NumJobsExecuted));
    这样,你会猜想出,当Job触发器触发时(在某个时刻),Execute (..)就被scheduler所调用。JobExecutionContext对象被传递给这个方法,它为Job实例提供了它的“运行时”环境-一个指向执行这个IJob实例的Scheduler句柄,一个指向触发该次执行的触发器的句柄,IJob的JobDetail对象以及一些其他的条目。
JobDetail对象由Quartz客户端在Job被加入到scheduler时创建。它包含了Job的各种设置属性以及一个JobDataMap对象,这个对象被用来存储给定Job类实例的状态信息。
      Trigger对象被用来触发jobs的执行。你希望将任务纳入到进度,要实例化一个Trigger并且“调整”它的属性以满足你想要的进度安排。Triggers也有一个JobDataMap与之关联,这非常有利于向触发器所触发的Job传递参数。Quartz打包了很多不同类型的Trigger,但最常用的Trigge类是SimpleTrigger和CronTrigger。
SimpleTrigger用来触发只需执行一次或者在给定时间触发并且重复N次且每次执行延迟一定时间的任务。CronTrigger按照日历触发,例如“每个周五”,每个月10日中午或者10:15分。
为什么要分为Jobs和Triggers?很多任务日程管理器没有将Jobs和Triggers进行区分。一些产品中只是将“job”简单地定义为一个带有一些小任务标识的执行时间。其他产品则更像Quartz中job和trigger的联合。而开发Quartz的时候,我们决定对日程和按照日程执行的工作进行分离。(从我们的观点来看)这有很多好处。
例如:jobs可以被创建并且存储在job scheduler中,而不依赖于trigger,而且,很多triggers可以关联一个job.另外的好处就是这种“松耦合”能使与日程中的Job相关的trigger过期后重新配置这些Job,这样以后就能够重新将这些Job纳入日程而不必重新定义它们。这样就可以更改或者替换trigger而不必重新定义与之相连的job标识符。
当向Quartz scheduler中注册Jobs 和Triggers时,它们要给出标识它们的名字。Jobs 和Triggers也可以被放入“组”中。“组”对于后续维护过程中,分类管理Jobs和Triggers非常有用。Jobs和Triggers的名字在组中必须唯一,换句话说,Jobs和Triggers真实名字是它的名字+组。如果使Job或者Trigger的组为‘null’,这等价于将其放入缺省的Scheduler.DEFAULT_GROUP组中。

现在对什么是Jobs 和 Triggers有了一般性的认识,可以通过第三课:更多关于Jobs和JobDetails的内容及第四课:关于Triggers更多的内容来深入地学习它们。

自由、创新、研究、探索……

转载于:https://blog.51cto.com/shanyou/74130

Quartz.net官方开发指南 第二课:Jobs And Triggers相关推荐

  1. Quartz.net官方开发指南 第九课: JobStore

    JobStore负责保持对所有scheduler "工作数据"追踪,这些工作数据包括:job(任务),trigger(触发器),calendar(日历)等.为你的Quartz sc ...

  2. Quartz.net官方开发指南 第十课: 配置、资源使用以及SchedulerFactory

    Quartz以模块方式构架,因此,要使它运行,几个组件必须很好的咬合在一起.幸运的是,已经有了一些现存的助手可以完成这些工作. 在Quartz进行工作之前需要被配置的组件主要有: • ThreadPo ...

  3. Quartz.net官方开发指南 第七课 : TriggerListeners和JobListeners

    监听器是在scheduler事件发生时能够执行动作的对象.可以看出,TriggerListeners接收与triggers相关的事件,而JobListeners则接收与Job相关的事件.<?xm ...

  4. Quartz.net官方开发指南 第五课: SimpleTrigger

    如果需要让任务只在某个时刻执行一次,或者,在某个时刻开始,然后按照某个时间间隔重复执行,简单地说,如果你想让触发器在<?xml:namespace prefix = st1 ns = " ...

  5. [翻译]现代java开发指南 第二部分

    现代java开发指南 第二部分 第二部分:部署.监控 & 管理,性能分析和基准测试 第一部分,第二部分,第三部分 =================== 欢迎来到现代 Java 开发指南第二部 ...

  6. OpenCart 官方开发指南翻译一 —— 模块开发

    模块开发   编写 OpenCart 模块可以很好地了解 OpenCart 如何运作的基本原理.就像 OpenCart 的其余部分一样,模块遵循 MVCL 设计模式.本文档指南将介绍如何使用 MVC- ...

  7. Vert.x Java开发指南——第二章 使用Vert.x编写最小可用Wiki

    第二章 使用Vert.x编写最小可用Wiki 版权声明:本文为博主自主翻译,转载请标明出处. https://blog.csdn.net/elinespace/article/details/8037 ...

  8. python开发中文软件-Python 3程序开发指南(第二版)

    2011年2月出版的,绝对最新哦,扫描绝对超级清楚. 在本书中,一流的Python程序员Mark Summerfield展示了如何充分利用Python 3的功能与特性来编写代码.与以前的版本相比.Py ...

  9. Hyperledger Fabric 1.0 实战开发系列 第二课 Fabric环境搭建

    一.安装GO语言 下载最新版的go 打开Terminal,输入命令(以下命令都是以root管理员的角色进行的) su 输入密码:***** wget https://storage.googleapi ...

最新文章

  1. java post接口测试_接口测试——Java + TestNG 国家气象局接口(json解析)实例
  2. 邮件服务器 之 基于FreeBSD和Postfix的邮件系统与邮件列表的web mail安装
  3. linux centos7安装git服务器配置,CentOS7 Linux环境下搭建Git仓库
  4. java中垃圾收集_Java中的垃圾收集器是什么?
  5. 按插入顺序排序的map
  6. Leetcode--268. 缺失数字
  7. 43岁被裁员,200万年薪泡汤:这4件事你要尽早明白
  8. ssm中使用slf4g
  9. String 字符串
  10. Python中计算文件的MD5值
  11. 用CentOS 6快速配置一台企业级Web代理服务器
  12. Flutter高级第3篇:底部 Tab 切换保持页面状态的几种方法
  13. 初识Python导图笔记
  14. vlan未能连接服务器,PC单机局域网连接VLAN的方法
  15. 全国高校经纬度(txt版)
  16. 所有的 Python 库都整理
  17. 北京大兴国际机场希尔顿花园酒店开业
  18. 计算机技术在现代地球科学中的重要性,浅谈GIS技术在地球科学中的应用.doc
  19. HI3861学习笔记(19)——WiFi接口使用(STA和AP模式)
  20. Mulitisim频率计设计

热门文章

  1. cocos studio和cocos creator关系
  2. 《数字视频和高清:算法和接口》一第1章 光 栅 图 像
  3. binlog日志的三种模式
  4. windows 下搭建Web服务器
  5. LeetCode第九题—— Palindrome Number(判断回文数)
  6. 流程 - 什么是真正的Scrum?
  7. [Android Pro] 分析 Package manager has died
  8. Windows下的鱿鱼(Squid)
  9. KVM为虚拟机添加设备总结
  10. How to use tcpdump with examples