当前位置:网站首页>Quartz. Net getting started
Quartz. Net getting started
2022-06-21 05:59:00 【Wind god Shura envoy】
Preface
stay ASP.NET The common scheduling framework in is nothing more than Quartz.NET And Hangfire Two kinds of , In the past, I often used to develop Hangfire With its background management interface , It is very convenient and easy to use , Recently, I have encountered some problems in new projects schedule The needs of , Colleagues pushed Quartz.Net To work as a job scheduler ,Quartz.Net It is a fully functional work scheduling framework , from Java Popular scheduling framework Quartz Migration to .NET On ,open source And provide flexible settings for developers to use , In the new 3.0.7 Support .NET Core 2.1 edition , Today, let's briefly introduce Quartz.NET Installation and basic application of , If there are any problems or mistakes, you are welcome to give guidance .
install
First , First create a file named QuartzNetConsole Of Console Project , Then open Nuget Package Mnage Input “quartz” Search for , Install the latest version of Quartz.NET Kit 
Or in Nuget Package Console Input instruction
Install-Package Quartz
If there is Json Serialize requirements , You can also add Quartz.Serialization.Json
Use
Before using, introduce Quartz.Net A few important API And Interface
IScheduler: Main work schedule API、 through Start Method run Scheduling .JobBuilder、IJobDetail: throughJobBuilder.CreateproduceIJobDetailOfInstance.TriggerBuilder、ITrigger: throughTriggerBuilder.CreateproduceITriggerOfInstance.
IJob : The custom schedule category needs to be implemented Interface
The diagram is as follows 
.png)]
And then Console Project Program.cs Medium Main Add the following code
class Program
{
static void Main(string[] args)
{
// trigger async evaluation
RunProgram().GetAwaiter().GetResult();
}
private static async Task RunProgram()
{
try
{
// establish scheduler
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
// establish Job
IJobDetail job = JobBuilder.Create<ShowDataTimeJob>()
.WithIdentity("job1", "group1")
.Build();
// establish Trigger, Run once per second
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(1)
.RepeatForever())
.Build();
// Join in ScheduleJob in
await scheduler.ScheduleJob(job, trigger);
// start-up
await scheduler.Start();
// perform 10 second
await Task.Delay(TimeSpan.FromSeconds(10));
// say goodbye
await scheduler.Shutdown();
}
catch (SchedulerException se)
{
await Console.Error.WriteLineAsync(se.ToString());
}
}
}
Which is to be executed ShowDataTimeJob The category code is as follows
internal class ShowDataTimeJob :IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync($" present time {
DateTime.Now}");
}
}
Program description
establish scheduler : through
factory.GetScheduler()obtainscheduleestablish
Job: UseJobBuilder.CreateestablishShowDataTimeJob, And define its key And group nameestablish
Trigger: UseTriggerBuilderestablishITrigger, Define its key And group name , And set the immediate execution time to execute once every second , amongShowDataTimeJobOfExecuteThis is the execution . The results are as follows :

feeling
Through simple description and operation , You can quickly create Quartz.NET The function of work scheduling , But in fact, the requirements and implementation code in real life are often not so simple , If you are interested, you can first refer to the development instructions of the official website , I will share it with you in the future ,Happy Coding .
in addition ,Quartz.Net It can also be used with other kits to set the profile XML In the file , It can even be matched with a little hand-made spirit , Hand engrave some code and place the profile in YAML In the file , Convenient configuration . Besides ,JOB The definition and implementation cycle of can also be achieved by installing other packages , Store it SQL Server or Redis in , Get a more flexible configuration . Or, , It can be placed in... Like a profile YAML In the file .
边栏推荐
猜你喜欢

应用在电视触摸屏中的十四通道智能触摸芯片

Interprocess communication (IPC): semaphores

Excel column numeric index to character index

Ironocr 2022.1.0 supports 127 national languages

sqli-labs23

基于注意力的seq2seq模型

Distributed transaction of microservices Seata

【JVM】 类加载器(ClassLoader)

Emotron Elton soft starter maintenance msf370/msf450

NumPy的广播机制
随机推荐
tf.AUTO_REUSE作用
The most popular explanation of librosa 𞓜 Mel spectrum
BGP - route announcement and reflector (explanation + configuration command)
el-table表格循环升级版
sqli-labs23
lambda-stream
El table remove the scroll bar and Zebra Stripe color modification
Research and Analysis on the current situation of China's revenue operation service market and forecast report on its development prospects (2022)
Subtitle, key frame animation and sound processing in PR
SSH copy ID batch password free script
Emotron Elton soft starter maintenance msf370/msf450
科创人·酷渲科技创始人华少:用双赢思维做产品、连生态,实现规模化发展
用代码生成器 生成代码后复制完成,在网页上不显示模块
Picture steganography: Method 1
[Prometheus] an optimization record of Prometheus Federation
Deep thinking caused by a namenode RPC delay troubleshooting
tf. compat. v1.global_ variables_ initializer
完善业务细节
【MYSQL】MySQL的SQL语句执行流程
Distributed transaction of microservices Seata