zoukankan      html  css  js  c++  java
  • Quartz.Net的基础使用方法,多任务执行

    前一篇随笔讲了Quartz单任务的简单实现,这一篇我们来讲讲多任务的实现

    Quartz.Net的基础使用方法,单任务执行

    主要看下面这段代码,这是Quartz多任务调度的方法,主要就是围绕这个方法去扩展:

    //
    // 摘要:
    //     Schedule all of the given jobs with the related set of triggers.
    //
    // 言论:
    //     If any of the given jobs or triggers already exist (or more specifically, if
    //     the keys are not unique) and the replace parameter is not set to true then an
    //     exception will be thrown.
    Task ScheduleJobs(IReadOnlyDictionary<IJobDetail, IReadOnlyCollection<ITrigger>> triggersAndJobs, bool replace, CancellationToken cancellationToken = default);

    1、首先我们建两个任务类:

    using System;
    using System.Threading.Tasks;
    using Quartz;
    
    namespace HHF.Quartz
    {
        public class Task_1 : IJob
        {
            public Task Execute(IJobExecutionContext context)
            {
                return Console.Out.WriteLineAsync($"这是任务一,执行时间:{DateTime.Now}");
            }
        }
    }

    2、改造一下之前的QuartzScheduler类,添加一个GetJobs方法,生成任务集合

     1 /// <summary>
     2 /// 很Low的一段代码
     3 /// </summary>
     4 /// <returns></returns>
     5 public List<IJobDetail> GetJobs()
     6 {
     7    IJobDetail job1 = JobBuilder.Create<Task_1>()
     8             .WithIdentity("job1")
     9             .Build();
    10    IJobDetail job2 = JobBuilder.Create<Task_2>()
    11             .WithIdentity("job3")
    12             .Build();
    13    return new List<IJobDetail>() { job1, job2 };
    14 }

    3、改造一下Run方法

     1 /// <summary>
     2 /// 任务调度的使用过程
     3 /// </summary>
     4 /// <returns></returns>
     5 public async static Task Run()
     6 {
     7     // 创建scheduler的引用
     8     ISchedulerFactory schedFact = new StdSchedulerFactory();
     9     IScheduler sched = await schedFact.GetScheduler();
    10 
    11     // 获取job集合
    12     var jobs = GetJobs();
    13     // 申明一个任务与触发器映射的字典集合
    14     var jobAndTriggerMapping = new Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>>();
    15     // 遍历任务列表
    16     for (int i = 0; i < jobs.Count; i++)
    17     {
    18         var job = jobs[i];
    19         // 只读的触发器集合
    20         var triggers = new ReadOnlyCollection<ITrigger>(
    21             new List<ITrigger>()
    22             {
    23                 TriggerBuilder.Create()
    24                     .WithIdentity("trigger_"+i)
    25                     .WithSimpleSchedule(x => x.WithIntervalInSeconds(i+5).RepeatForever())
    26                     .Build()
    27             });
    28         // 建立映射关系
    29         jobAndTriggerMapping[job] = triggers;
    30     }
    31     // 将映射关系包装成制度字典集合
    32     var readOnlyjobAndTriggerMapping = new ReadOnlyDictionary<IJobDetail, IReadOnlyCollection<ITrigger>>(jobAndTriggerMapping);
    33 
    34     /* 
    35      * 使用trigger规划执行任务job
    36      *第二个参数replace:如果为true,则指定的触发器或者任务名称已经存在将会替换,否则将抛出异常
    37      */
    38     await sched.ScheduleJobs(readOnlyjobAndTriggerMapping, true);
    39 
    40     // 启动 scheduler
    41     await sched.Start();
    42 }

    4、查看执行结果,我们可以看到,任务一的执行间隔时间为5秒,任务二的执行间隔时间为6秒,可以正常执行。

  • 相关阅读:
    2013面试C++小结
    Linux C 面试题总结 .
    [SCOI2011]糖果
    python——简单爬虫
    python——ADSL拨号程序
    python——处理xls表格
    Vsphere初试——架设Panabit行为管理
    Vsphere初试——使用Vsphere client
    Vsphere初试——基本安装
    Python2与Python3的不同点
  • 原文地址:https://www.cnblogs.com/cool-net/p/13472723.html
Copyright © 2011-2022 走看看