当前位置:网站首页>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
边栏推荐
- Ide-idea-problem
- [mock data] fastmock dynamically returns the mock content according to the incoming parameters
- C语言实现通讯录----(静态版本)
- [Mysql] LEFT函數 | RIGHT函數
- 准备一个月去参加ACM,是一种什么体验?
- Find the number of leaf nodes of binary tree
- 荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
- Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
- General test technology [II] test method
- 求二叉树的叶子结点个数
猜你喜欢
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
编码电机PID调试(速度环|位置环|跟随)
Comprehensive calculation of employee information
类似Jira的十大项目管理软件
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
ASP. Net 6 middleware series - Custom middleware classes
随机推荐
ASP. Net 6 middleware series - Custom middleware classes
Configuration table and page information automatically generate curd operation page
General test technology [II] test method
Knowledge of software testing~
How does Microsoft solve the problem of multiple PC programs
Preview of converting doc and PDF to SWF file
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
xutils3修改了我提报的一个bug,开心
The most easy to understand dependency injection and control inversion
What kind of experience is it to prepare for a month to participate in ACM?
[Mysql] LEFT函数 | RIGHT函数
2022G2电站锅炉司炉考试题库及在线模拟考试
Five tips for cross-border e-commerce in 2022
类似Jira的十大项目管理软件
C语言实现通讯录----(静态版本)
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
先中二叉建树
可以接收多种数据类型参数——可变参数