zoukankan      html  css  js  c++  java
  • .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 <quartz> 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"?>
    <!-- This file contains job definitions in schema version 2.0 format -->
    <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>
        <!--定义示例任务1 Job-->
        <job>
          <name>ItemJob</name>
          <group>ItemJobGroup</group>
          <description>任务</description>
          <!--job-type,完整类名,命名空间,用逗号隔开-->
          <job-type>ConsoleQuartz.HelloQuartzJob,ConsoleQuartz</job-type>
          <durable>true</durable>
          <recover>false</recover>
        </job>
        <!--定义示例任务1 触发器 每3秒执行一次 ItemJob 任务-->
        <trigger>
          <cron>
            <name>ItemJobTrigger</name>
            <group>DItemJobTriggerGroup</group>
            <!--指定JOB名-->
            <job-name>ItemJob</job-name>
            <!--指定JOB组-->
            <job-group>ItemJobGroup</job-group>
            <cron-expression>0/3 * * * * ?</cron-expression>
          </cron>
        </trigger>
        
        <!--定义示例任务2 Job-->
        <job>
          <name>ItemJob2</name>
          <group>ItemJobGroup2</group>
          <description>任务2</description>
          <!--job-type,完整类名,命名空间,用逗号隔开-->
          <job-type>ConsoleQuartz.HelloQuartzJob2,ConsoleQuartz</job-type>
          <durable>true</durable>
          <recover>false</recover>
        </job>
        <!--定义示例任务2 触发器 每4秒执行一次 ItemJob2 任务-->
        <trigger>
          <cron>
            <name>ItemJobTrigger2</name>
            <group>DItemJobTriggerGroup2</group>
            <job-name>ItemJob2</job-name>
            <job-group>ItemJobGroup2</job-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

  • 相关阅读:
    error C2143: syntax error : missing ';' before 'type'
    【剑指Offer面试编程题】题目1360:乐透之猜数游戏--九度OJ
    【剑指Offer面试编程题】题目1362:左旋转字符串--九度OJ
    【剑指Offer面试编程题】题目1361:翻转单词顺序--九度OJ
    【剑指Offer面试编程题】题目1354:和为S的连续正数序列--九度OJ
    【剑指Offer面试编程题】 题目1350:二叉树的深度--九度OJ
    Oracle个人自学笔记
    【转载】Oracle sqlplus中最简单的一些命令,设置显示的格式
    IntelliJ IDEA常用快捷键大全
    凤凰系统(Phoenix OS)PC版安装,电脑上体验功能丰富的安卓系统
  • 原文地址:https://www.cnblogs.com/runliuv/p/15636874.html
Copyright © 2011-2022 走看看