.Net Core使用Quartz

一、配置文件

        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()
    {
        //具体逻辑
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值