当前位置:网站首页>Quartz. Www. 18fu Used in net core
Quartz. Www. 18fu Used in net core
2022-04-23 03:16:00 【Pingshan CP3】
Recent use Quartz.net To achieve scheduled job scheduling , Now record the use process .
About Quartz Introduction and detailed description of , Please visit the official website https://www.quartz-scheduler.net/
One 、Quartz.net There are mainly the following objects in
1.StdSchedulerFactory( Dispatcher factory ) Used to create a schedule
2.IScheduler ( Scheduler ) Used to bind tasks and triggers to work
3.IJobDetail( Mission ) Used to perform business work
4.ITrigger( trigger ) Used to perform tasks under specific conditions
Two 、 install Quartz.AspNetCore package

3、 ... and 、Quartz.net There are two ways to implement job scheduling , Configure document mode and do not configure document mode , The following will demonstrate .
1. There is no need to configure the document mode :
If your homework project is relatively simple , There is usually only one task or one or two triggers , And it will not change the trigger strategy or add tasks in the future , Then you only need a few lines of code to use Quartz Just fine .
public static void Main(string[] args)
{
// Mission ,JobBLL That is, your task class
IJobDetail jobDetail = JobBuilder.Create<MyJob>().WithIdentity("MyJob").Build();
//cron Time strategy
ITrigger trigger = TriggerBuilder.Create().WithIdentity("Mytrigger").WithCronSchedule(CommonBLL.Config["system:cron"]).Build();
// Execute the policy once
ITrigger nowTrigger = TriggerBuilder.Create().WithIdentity("now").StartNow().Build();
// Simple strategy , Every time 3 Once per second , Two times in all
//ITrigger trigger = TriggerBuilder.Create().WithSimpleSchedule(s => s.WithIntervalInSeconds(3).WithRepeatCount(2)).Build();
// Start scheduling
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
//await scheduler.ScheduleJob(jobDetail, trigger);
// binding job and trigger, Add tasks and triggers to the scheduler
await scheduler.ScheduleJob(jobDetail, new List<ITrigger>() { trigger, nowTrigger }, replace: true);// Multiple trigger, Execute now && According to the established cron Time strategy
//scheduler.ListenerManager.AddJobListener(new TestJobListener());// Add task execution monitoring
await scheduler.Start();// Turn on scheduling
}
[DisallowConcurrentExecution]// It is forbidden to perform tasks at the same time
public class MyJob:IJob
{
// Realization Execute Method
public Task Execute(IJobExecutionContext context)
{
return Task.Run(() =>
{
Console.WriteLine("begin....");
// Your homework part
Thread.Sleep(3000);
Console.WriteLine("end...");
});
}
}
among ,IJob The implementation of is relatively simple , Put the business code in Job Medium Execute In the method .
Trigger It is divided into simple trigger and Cron The way , Simple triggers generally refer to immediate execution , Or every other time ;Cron Means that... Can be used Cron Expression to specify the trigger time ;Cron You can find tools to construct expressions on the Internet :https://cron.qqe2.com/
2. Configure document mode
The way to configure documents is to configure... In the configuration file trigger And binding strategy , This enables flexible configuration ;
(1) New configuration file quartz.config
# You can configure your scheduler in either <quartz> configuration section
# or in quartz properties file
# Configuration section has precedence
quartz.scheduler.instanceName = QuartzTest
# configure thread pool info
quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
quartz.threadPool.threadCount = 10
quartz.threadPool.threadPriority = Normal
# job initialization plugin handles our xml reading, without it defaults are used
# quartz.plugin.triggHistory.type = Quartz.Plugin.History.LoggingJobHistoryPlugin
quartz.plugin.jobInitializer.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins
# Configure document path
quartz.plugin.jobInitializer.fileNames = quartz_jobs.xml
# export this server to remoting context
#quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
#quartz.scheduler.exporter.port = 555
#quartz.scheduler.exporter.bindName = QuartzScheduler
#quartz.scheduler.exporter.channelType = tcp
#quartz.scheduler.exporter.channelName = httpQuartz
It's mainly quartz.plugin.jobInitializer.fileNames This attribute is the path of the next configuration file , In addition, copy the configuration file to the output directory and set it to “ Always copy ” perhaps “ Newer assignment ”, Only in this way can we generate debug or release in .

(2) Add configuration file quartz_jobs.xml
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0">
<processing-directives>
<overwrite-existing-data>true</overwrite-existing-data>
</processing-directives>
<schedule>
<job>
<name>MyJob</name>
<description>MyJob Test task </description>
<!-- Specify the job implementation class , The format is namespace name . Class name , Assembly name -->
<job-type>QuartzConsole.MyJob, QuartzConsole</job-type>
</job>
<job>
<name>HisJob</name>
<description>HisJob Test task </description>
<job-type>QuartzConsole.HisJob, QuartzConsole</job-type>
</job>
<trigger>
<simple>
<name>simpleTrigger</name>
<description>TestTrigger Test triggers </description>
<job-name>MyJob</job-name>
<!-- Number of job repetitions ,-1 Indicates unlimited repeated execution -->
<repeat-count>-1</repeat-count>
<!-- Job trigger interval ,1000 Means to execute once a second -->
<repeat-interval>3000</repeat-interval>
</simple>
</trigger>
<trigger>
<cron>
<name>cronTrigger</name>
<description>TestTrigger1 Test triggers </description>
<job-name>MyJob</job-name>
<!--cron expression -->
<cron-expression>0/2 * * * * ?</cron-expression>
</cron>
</trigger>
<trigger>
<cron>
<name>cronTrigger1</name>
<description>TestTrigger1 Test triggers </description>
<job-name>HisJob</job-name>
<cron-expression>0/1 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
Configure according to the configuration section , among <job> Task attribute in ,trigger For trigger properties , The specific meaning has been explained in the notes , Of course, there are many configuration variables , such as job-group perhaps trigger-group, Not used yet , So don't use ;
(3) Start scheduling
static void Main(string[] args)
{
// Dispatch
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();// Turn on scheduling
}
So when scheduling is turned on , Schedule yourself to detect tasks and triggers in the configuration file ; If you add or modify later job perhaps trigger When , You can operate in the configuration file .
End , More functions will be described later
版权声明
本文为[Pingshan CP3]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627117321.html
边栏推荐
- 12.<tag-链表和常考点综合>-lt.234-回文链表
- Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
- C introduction of variable parameter params
- Xutils3 corrected a bug I reported. Happy
- ASP. Net 6 middleware series - Custom middleware classes
- Docker拉取mysql并连接
- ASP. Net 6 middleware series - conditional Middleware
- js递归树结构计算每个节点的叶子节点的数量并且输出
- General test technology [II] test method
- Fiddler use
猜你喜欢

Knowledge of software testing~

After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path

《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案

Maui initial experience: Cool

Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

svg标签中利用<polygon/>循环数组绘制多边形

ASP. Net 6 middleware series - execution sequence

Xamarin effect Chapter 21 expandable floating operation button in GIS

ASP. Net 6 middleware series - Custom middleware classes
随机推荐
編碼電機PID調試(速度環|比特置環|跟隨)
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
OLED多级菜单记录
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
A set of C interview questions about memory alignment. Many people make mistakes!
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
Blazor University (11) component - replace attributes of subcomponents
The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
2022a special equipment related management (elevator) work license question bank and simulation examination
C read / write binary file
MySQL installation pit
Use split to solve the "most common words" problem
General testing technology [1] classification of testing
Xutils3 corrected a bug I reported. Happy
How does Microsoft solve the problem of multiple PC programs
Fiddler use
C introduction of variable parameter params
[Mysql] LEFT函数 | RIGHT函数
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure