Lesson 1: Using Quartz
课程1:使用Quartz
Before you can use the scheduler, it needs to be instantiated (who’d have guessed?). To do this, you use an implementor of ISchedulerFactory.
在使用调度器之前,它必须被实例化(谁猜到了呢?)。要实现它,你可以使用一个ISchedulerFactory工厂接口的实现。
Once a scheduler is instantiated, it can be started, placed in stand-by mode, and shutdown. Note that once a scheduler is shutdown, it cannot be restarted without being re-instantiated. Triggers do not fire (jobs do not execute) until the scheduler has been started, nor while it is in the paused state.
一旦调度器被实例化,它就能被启动,挂起和关闭。必须注意一旦调度器被关闭,要启动它必须要重新实例化。触发器在调度器没有启动之前或者处于暂停状态时不会执行触发动作(作业不会被执行)。
Here’s a quick snippet of code, that instantiates and starts a scheduler, and schedules a job for execution:
下面是一个代码的小范例,范例中实现了调度器的实例化和启动,以及对一项作业的调度。
Using Quartz.NET
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(40)
.RepeatForever())
.Build();
sched.ScheduleJob(job, trigger);
As you can see, working with Quartz.NET is rather simple. In Lesson 2 we’ll give a quick overview of Jobs and Triggers, so that you can more fully understand this example.
就像你看到的,使用Quart.NET框架十分的简单,在第二课我们会给出一个作业和触发器的快速概览,让你可以全面的理解这个范例。
希望我的文字能给你带来帮助: