当前位置: 首页 > 工具软件 > Quartz EX > 使用案例 >

.Net Core使用Quartz

司寇季
2023-12-01

一、配置文件

        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
 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

        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>DataCollectJob</name>
      <group>DataCollectJobGroup</group>
      <description>MyJob测试任务</description>
      <job-type>QuartzJob.DataCollectJob, QuartzJob</job-type>
      <durable>true</durable>
      <recover>false</recover>
    </job>
    <trigger>
      <simple>
        <name>DataCollectJobTrigger</name>
        <group>DataCollectJobGroup</group>
        <description>TestTrigger测试触发器</description>
        <job-name>DataCollectJob</job-name>
        <job-group>DataCollectJobGroup</job-group>
        <repeat-count>-1</repeat-count>
        <repeat-interval>100000</repeat-interval>
      </simple>
    </trigger>
  </schedule>
</job-scheduling-data>

二、Program.cs代码

using Quartz;
using Quartz.Impl;
using System;
using System.Threading.Tasks;


namespace QuartzJob
{
    class Program
    {
        private static void Main(string[] args)
        {
            Task.Run(() => { Run().GetAwaiter(); });


            Console.ReadKey();
        }
        public static async Task Run()
        {
            try
            {
                ISchedulerFactory sf = new StdSchedulerFactory();
                IScheduler scheduler = await sf.GetScheduler();
                Console.WriteLine("start the schedule");
                await scheduler.Start();
                Console.WriteLine("end");
            }
            catch (SchedulerException se)
            {
                await Console.Error.WriteLineAsync(se.ToString());
            }
            catch (Exception ex)
            {
                Console.Write($"err={ex.ToString()}");
            }
        }
    }
}

三、具体的Job逻辑

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using System;
using System.Data;
using System.Text;
using System.Threading.Tasks;


namespace QuartzJob
{
    //DisallowConcurrentExecution能保证上一个周期的任务还没执行完之前,下一个周期到来了就会被忽略
    [DisallowConcurrentExecution]
    public class DataCollectJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                Init();
            });
        }
    }
    
    public void Init()
    {
        //具体逻辑
    }
}

 类似资料: