首页 > 其他分享 >Quartz.net官方开发指南 第二课:Jobs And Triggers

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

时间:2022-12-15 20:33:08浏览次数:35  
标签:Info Quartz Jobs 第二课 will job trigger group1 log

JobDetail对象由Quartz客户端在Job被加入到scheduler时创建。它包含了Job的各种设置属性以及一个JobDataMap对象,这个对象被用来存储给定Job类实例的状态信息。 Trigger对象被用来触发jobs的执行。你希望将任务纳入到进度,要实例化一个Trigger并且“调整”它的属性以满足你想要的进度安排。Triggers也有一个JobDataMap与之关联,这非常有利于向触发器所触发的Job传递参数。Quartz打包了很多不同类型的Trigger,但最常用的Trigge类是SimpleTrigger和CronTrigger。

正如前面所提到的那样,通过实现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);

}

}


         

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更多的内容来深入地学习它们。



标签:Info,Quartz,Jobs,第二课,will,job,trigger,group1,log
From: https://blog.51cto.com/shanyou/5946039

相关文章

  • .netCore 使用 Quartz 实例
    一、参考源文链接 1、https://www.likecs.com/show-897836.html2、https://blog.csdn.net/weixin_43614067/article/details/115373776二、Quartz基本使用publiccla......
  • Spring Boot整合Quartz实现定时任务表配置
    最近有个小项目要做,springmvc下的task设置一直不太灵活,因此在SpringBoot上想做到灵活的管理定时任务。需求就是,当项目启动的时候,如果有定时任务则加载进来,生成scheduler,通......
  • 框架第二课---静态文件配置,request对象方法,pycharm连接MySQL,django连接MySQL,django模
    昨日内容回顾手写web框架1.socket服务端2.http协议3.网址后缀wsgiref模块1.封装socket代码2.处理http相关数据代码封装优化1.函数2.对应关系3.文件、目......
  • Configuration of your jobs with .gitlab-ci.yml
    Thisdocumentdescribestheusageof.gitlab-ci.yml,thefilethatisusedbyGitLabRunnertomanageyourproject'sjobs.Fromversion7.12,GitLabCIusesaYA......
  • springboot2 搭建日志收集系统存入mongodb + redis+mq+线程池+xxljobs
    我们看到了高效批量插入mongodb速度还不错,那么我们的系统日志收集怎么做呢。当然当前文件日志收集效果也不错,比如前面博文写的elkf搭建日志收集系统。但我们系统中总是有......
  • C语言第二课学习笔记--运算符,流程控制和函数
    <7>运算符1)分类:一些基本的运算符分类有:算术、关系、逻辑、赋值算术运算符有:+,-,*,/,%;    对于/被除数和除数都是int则结果是int,有一个是float结果就是float;对于%,被......
  • Quartz使用监听器插入定时任务执行日志
    Quartz使用监听器插入定时任务执行日志使用springboot,将监听器交给spring容器管理,并像其中注入日志服务类,环境准备工作实现任务调度需要导入两个quartz的maven依赖<depen......
  • Quartz深度实战
    概述Java语言中最正统的任务调度框架,几乎是首选。后来和SpringSchedule平分秋色;再后来会被一些轻量级的分布式任务调度平台,如XXL-Job取代。另外近几年Quartz的维护和发布几......
  • 吴恩达课程学习笔记--第二课第一周:深度学习的实践层面
    训练,验证,测试在机器学习的小数据时代,70%验证集,30%测试集,或者60%训练,20%验证和20%测试。大数据时代,如果有百万条数据,我们可以训练集占98%,验证测试各占1%。深度学习的一个趋......
  • 前端第二课---
    昨日内容回顾前端与后端的概念前端类似于前台接待后端类似于幕后决策HTTP协议1.四大特性 1.基于请求响应 2.基于TCP、IP作用于应用层之上的协议 3.无状态 ......