.NET6控制台程序使用quartz.net

1.新建一个名为“ConsoleQuartz”的.NET6控制台程序。

2.nuget中安装Quartz和Quartz.Plugins,这2个DLL。

3.新建一个HelloQuartzJob类:

using Quartz;

namespace ConsoleQuartz
{
    public class HelloQuartzJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Hello Quartz.Net");
            });
        }
    }
}

4.新建一个HelloQuartzJob2:

using Quartz;

namespace ConsoleQuartz
{
    public class HelloQuartzJob2 : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Hello HelloQuartzJob2");
            });
        }
    }
}

5.修改Program.cs:

// See https://aka.ms/new-console-template for more information
using Quartz;
using Quartz.Impl;

Console.WriteLine("Hello, World!");

await MainAsync();
Console.ReadKey();

static async Task MainAsync()
{
    IScheduler Scheduler=await StdSchedulerFactory.GetDefaultScheduler();
    await Scheduler.Start();     
    Console.WriteLine("任务调度器已启动");
}

6.在编译输出目录(bin\Debug\net6.0)下新建一个quartz.config,内容如下:

# You can configure your scheduler in either  configuration section
# or in quartz properties file
# Configuration section has precedence

quartz.scheduler.instanceName = ServerScheduler

# 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.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins
quartz.plugin.xml.fileNames = ~/quartz_jobs.xml

# export this server to remoting context
quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
quartz.scheduler.exporter.port = 5552
quartz.scheduler.exporter.bindName = QuartzScheduler
quartz.scheduler.exporter.channelType = tcp
quartz.scheduler.exporter.channelName = httpQuartz

注意quartz.plugin.xml.type,使用的是Quartz.Plugins,老版本是Quartz,如果写成

Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz

会报错:

Quartz.SchedulerException:“SchedulerPlugin of type 'Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz' could not be instantiated.”
TypeLoadException: Could not load type 'Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin' from assembly 'Quartz'.

 

7.在编译输出目录(bin\Debug\net6.0)下新建一个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>trueoverwrite-existing-data>
  processing-directives>
  <schedule>
    
    <job>
      <name>ItemJobname>
      <group>ItemJobGroupgroup>
      <description>任务description>
      
      <job-type>ConsoleQuartz.HelloQuartzJob,ConsoleQuartzjob-type>
      <durable>truedurable>
      <recover>falserecover>
    job>
    
    <trigger>
      <cron>
        <name>ItemJobTriggername>
        <group>DItemJobTriggerGroupgroup>
        
        <job-name>ItemJobjob-name>
        
        <job-group>ItemJobGroupjob-group>
        <cron-expression>0/3 * * * * ?cron-expression>
      cron>
    trigger>
    
    
    <job>
      <name>ItemJob2name>
      <group>ItemJobGroup2group>
      <description>任务2description>
      
      <job-type>ConsoleQuartz.HelloQuartzJob2,ConsoleQuartzjob-type>
      <durable>truedurable>
      <recover>falserecover>
    job>
    
    <trigger>
      <cron>
        <name>ItemJobTrigger2name>
        <group>DItemJobTriggerGroup2group>
        <job-name>ItemJob2job-name>
        <job-group>ItemJobGroup2job-group>
        <cron-expression>0/4 * * * * ?cron-expression>
      cron>
    trigger>
    
  schedule>
job-scheduling-data>

 

 

8.按F5启动程序:

Hello, World!
任务调度器已启动
Hello HelloQuartzJob2
Hello Quartz.Net
Hello HelloQuartzJob2
Hello Quartz.Net

 

你可能感兴趣的:(.NET6控制台程序使用quartz.net)