Quartz.net官方开发指南 第二课:Jobs And Triggers
{
/**//// <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);
}
}
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));
现在对什么是Jobs 和 Triggers有了一般性的认识,可以通过第三课:更多关于Jobs和JobDetails的内容及第四课:关于Triggers更多的内容来深入地学习它们。
转载于:https://blog.51cto.com/shanyou/74130
Quartz.net官方开发指南 第二课:Jobs And Triggers相关推荐
- Quartz.net官方开发指南 第九课: JobStore
JobStore负责保持对所有scheduler "工作数据"追踪,这些工作数据包括:job(任务),trigger(触发器),calendar(日历)等.为你的Quartz sc ...
- Quartz.net官方开发指南 第十课: 配置、资源使用以及SchedulerFactory
Quartz以模块方式构架,因此,要使它运行,几个组件必须很好的咬合在一起.幸运的是,已经有了一些现存的助手可以完成这些工作. 在Quartz进行工作之前需要被配置的组件主要有: • ThreadPo ...
- Quartz.net官方开发指南 第七课 : TriggerListeners和JobListeners
监听器是在scheduler事件发生时能够执行动作的对象.可以看出,TriggerListeners接收与triggers相关的事件,而JobListeners则接收与Job相关的事件.<?xm ...
- Quartz.net官方开发指南 第五课: SimpleTrigger
如果需要让任务只在某个时刻执行一次,或者,在某个时刻开始,然后按照某个时间间隔重复执行,简单地说,如果你想让触发器在<?xml:namespace prefix = st1 ns = " ...
- [翻译]现代java开发指南 第二部分
现代java开发指南 第二部分 第二部分:部署.监控 & 管理,性能分析和基准测试 第一部分,第二部分,第三部分 =================== 欢迎来到现代 Java 开发指南第二部 ...
- OpenCart 官方开发指南翻译一 —— 模块开发
模块开发 编写 OpenCart 模块可以很好地了解 OpenCart 如何运作的基本原理.就像 OpenCart 的其余部分一样,模块遵循 MVCL 设计模式.本文档指南将介绍如何使用 MVC- ...
- Vert.x Java开发指南——第二章 使用Vert.x编写最小可用Wiki
第二章 使用Vert.x编写最小可用Wiki 版权声明:本文为博主自主翻译,转载请标明出处. https://blog.csdn.net/elinespace/article/details/8037 ...
- python开发中文软件-Python 3程序开发指南(第二版)
2011年2月出版的,绝对最新哦,扫描绝对超级清楚. 在本书中,一流的Python程序员Mark Summerfield展示了如何充分利用Python 3的功能与特性来编写代码.与以前的版本相比.Py ...
- Hyperledger Fabric 1.0 实战开发系列 第二课 Fabric环境搭建
一.安装GO语言 下载最新版的go 打开Terminal,输入命令(以下命令都是以root管理员的角色进行的) su 输入密码:***** wget https://storage.googleapi ...
最新文章
- java post接口测试_接口测试——Java + TestNG 国家气象局接口(json解析)实例
- 邮件服务器 之 基于FreeBSD和Postfix的邮件系统与邮件列表的web mail安装
- linux centos7安装git服务器配置,CentOS7 Linux环境下搭建Git仓库
- java中垃圾收集_Java中的垃圾收集器是什么?
- 按插入顺序排序的map
- Leetcode--268. 缺失数字
- 43岁被裁员,200万年薪泡汤:这4件事你要尽早明白
- ssm中使用slf4g
- String 字符串
- Python中计算文件的MD5值
- 用CentOS 6快速配置一台企业级Web代理服务器
- Flutter高级第3篇:底部 Tab 切换保持页面状态的几种方法
- 初识Python导图笔记
- vlan未能连接服务器,PC单机局域网连接VLAN的方法
- 全国高校经纬度(txt版)
- 所有的 Python 库都整理
- 北京大兴国际机场希尔顿花园酒店开业
- 计算机技术在现代地球科学中的重要性,浅谈GIS技术在地球科学中的应用.doc
- HI3861学习笔记(19)——WiFi接口使用(STA和AP模式)
- Mulitisim频率计设计