Background
Many times, projects need to perform one or many different jobs at different times.
The Windows execution plan does not meet the requirements very well at this time. At this time, a more powerful, easy to manage, set deployment of job scheduling.
Introduced
Quartz an open source job scheduling framework, Opensymphony's open source project. Quartz.net is a C # ported version of Quartz.
It has some very good features:
1: Support cluster, job grouping, job remote management.
2: Customize fine-grained time triggers, using simple, job and trigger separations.
3: Database support, can host Windows services, Website,winform and so on.
Actual combat
One: some basic concepts.
Scheduler of the operating device.
IJob the job interface. Inherit and implement execute, writing the specific job logic for execution.
Jobbuilder generates a detailed job information (Jobdetail) based on the settings.
Triggerbuilder according to the rules, the production of corresponding trigger
Two: NuGet installs pm> install-package Quartz.
Three: Examples (detailed comments, not described separately).
12345678910111213141516171819202122232425262728293031323334353637383940414243 |
static
void
Main(
string
[] args)
{
//从工厂中获取一个调度器实例化
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//开启调度器
//==========例子1(简单使用)===========
IJobDetail job1 = JobBuilder.Create<HelloJob>()
//创建一个作业
.WithIdentity(
"作业名称"
,
"作业组"
)
.Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity(
"触发器名称"
,
"触发器组"
)
.StartNow()
//现在开始
.WithSimpleSchedule(x => x
//触发时间,5秒一次。
.WithIntervalInSeconds(5)
.RepeatForever())
//不间断重复执行
.Build();
scheduler.ScheduleJob(job1, trigger1);
//把作业,触发器加入调度器。
//==========例子2 (执行时 作业数据传递,时间表达式使用)===========
IJobDetail job2= JobBuilder.Create<DumbJob>()
.WithIdentity(
"myJob"
,
"group1"
)
.UsingJobData(
"jobSays"
,
"Hello World!"
)
.Build();
ITrigger trigger2 = TriggerBuilder.Create()
.WithIdentity(
"mytrigger"
,
"group1"
)
.StartNow()
.WithCronSchedule(
"/5 * * ? * *"
)
//时间表达式,5秒一次
.Build();
scheduler.ScheduleJob(job2, trigger2);
//scheduler.Shutdown(); //关闭调度器。
}
|
12345678910 |
/// <summary>
/// 作业
/// </summary>
public
class
HelloJob : IJob
{
public
void
Execute(IJobExecutionContext context)
{
Console.WriteLine(
"作业执行!"
);
}
}
|
12345678910111213141516 |
public
class
DumbJob : IJob
{
/// <summary>
/// context 可以获取当前Job的各种状态。
/// </summary>
/// <param name="context"></param>
public
void
Execute(IJobExecutionContext context)
{
JobDataMap dataMap = context.JobDetail.JobDataMap;
string
content = dataMap.GetString(
"jobSays"
);
Console.WriteLine(
"作业执行,jobSays:"
+ content);
}
}
|
Other
Withcronschedule ("") powerful time expression.
Withsimpleschedule (x) is generally enough.
Reference Resources
Quartz.net Official 2.X Tutorial http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/index.html
Quartz.net Open Source Address https://github.com/quartznet/quartznet
---------------------------------------------------------------------------------
Quartz is a opensymphony open source organization in the Job scheduling field another open source project, which can be combined with the Java EE and J2SE application can also be used alone. Quartz can be used to create a simple or complex day table that runs 10, hundreds, or even tens of thousands of jobs. Jobs can be made into standard Java components or EJBs.
Java version of the address: http://www.quartz-scheduler.org/documentation,. NET version of the address: http://quartznet.sourceforge.net/.
If manually set crons expression, more cumbersome, this article on the previous web-based Java Quartz Expression Generator source code packaging packaging,
The attachment can be downloaded, as follows:
: Quartz Expression Builder
NET job scheduling (i)-quartz.net Getting Started Quartz expression Builder [go]