Quartz.net Tutorial Lesson 1
第一课:使用Quartz.net
关键字:
IschedulerFactory:调度工厂
IScheduler:调度器
IJob:任务
JobDetail:任务明细
Trigger:任务触发器
在我们使用scheduler之前,它需要被实例化,因此,我们使用了一个IschedulerFactory的实现。
一旦scheduler被实例化,它可以被启动,并处于备用模式(我们可以理解为监听状态),并且可以被关闭, 注意:一旦一个scheduler被关闭,除非重新实例化,否则它无法重新启动。并且scheduler必须在启动状态,并且非暂停状态,triggers才可以执行。
下面是一段简单的实例代码,如何创建并启动一个scheduler实例,并且如何调度一个任务的执行。
config:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<quartz>
<add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
<add key="quartz.threadPool.threadCount" value="10"/>
<add key="quartz.threadPool.threadPriority" value="2"/>
<add key="quartz.jobStore.misfireThreshold" value="60000"/>
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
</quartz>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
jobImpl
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
namespace QuartzConsoleDemo
{
class PrintDate : IJob
{
public void Execute(JobExecutionContext context)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
}
}
class PrintHello : IJob
{
public void Execute(JobExecutionContext context)
{
Console.WriteLine("Hello, Guys!");
}
}
}
program
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Quartz.Impl;
namespace QuartzConsoleDemo
{
class Program
{
static void Main(string[] args)
{
// 创建调度工厂
ISchedulerFactory schedFact = new StdSchedulerFactory();
// 创建一个调度实例
IScheduler sched = schedFact.GetScheduler();
// 开始执行调度任务
sched.Start();
// 添加一个执行明细(任务)
JobDetail job = new JobDetail("PrintDate", null, typeof(PrintDate));
// 添加一个触发器(任务何时|何种条件下执行)
Trigger trig = new SimpleTrigger("trig");
Trigger trigger = TriggerUtils.MakeSecondlyTrigger(1);
trigger.StartTimeUtc = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
trigger.Name = "PrintTrigger";
// 将执行明细和触发器加入调度器的监控
sched.ScheduleJob(job, trigger);
JobDetail jobHello = new JobDetail("PrintHello", null, typeof(PrintHello));
Trigger trigger1 = TriggerUtils.MakeSecondlyTrigger(2);
trigger1.StartTimeUtc = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
trigger1.Name = "PrintHelloTrigger";
sched.ScheduleJob(jobHello, trigger1);
}
}
}
原文地址:http://quartznet.sourceforge.net/tutorial/lesson_1.html
Lesson 1: Using 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.
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(); // construct job info JobDetail jobDetail = new JobDetail("myJob", null, typeof(HelloJob)); // fire every hour Trigger trigger = TriggerUtils.MakeHourlyTrigger(); // start on the next even hour trigger.StartTimeUtc = TriggerUtils.GetEvenHourDate(DateTime.UtcNow); trigger.Name = "myTrigger"; sched.ScheduleJob(jobDetail, 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.