作业引擎quartz.net --- 监听链
针对多个作业:如何描述各个跑批任务之间的顺序,紧前、紧后关系,实现灵活调度。例如:A完成则B开始,B完成C开始。
对quartz.net 进行了查阅,能实现如上业务,如下图:
测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using Quartz.Listener;
namespace ConsoleApp
{
public class quartznetTest
{
public static void Run()
{
ISchedulerFactory factory = new StdSchedulerFactory();
// Get scheduler and add object
IScheduler scheduler = factory.GetScheduler();
JobKey firstJobKey = JobKey.Create("FirstJob", "Pipeline");
JobKey secondJobKey = JobKey.Create("SecondJob", "Pipeline");
JobKey thirdJobKey = JobKey.Create("ThirdJob", "Pipeline");
// Create job and trigger
IJobDetail firstJob = JobBuilder.Create<SimpleJob1>()
.WithIdentity(firstJobKey)
//.StoreDurably(true)
.Build();
IJobDetail secondJob = JobBuilder.Create<SimpleJob2>()
.WithIdentity(secondJobKey)
.StoreDurably(true)
.Build();
IJobDetail thirdJob = JobBuilder.Create<SimpleJob3>()
.WithIdentity(thirdJobKey)
.StoreDurably(true)
.Build();
ITrigger firstJobTrigger = TriggerBuilder.Create()
.WithIdentity("Trigger", "Pipeline")
.WithSimpleSchedule(x => x
.WithMisfireHandlingInstructionFireNow()
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
JobChainingJobListener listener = new JobChainingJobListener("Pipeline Chain");
listener.AddJobChainLink(firstJobKey, secondJobKey);
listener.AddJobChainLink(secondJobKey, thirdJobKey);
scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Pipeline"));
// Run it all in chain
scheduler.Start();
scheduler.ScheduleJob(firstJob, firstJobTrigger);
scheduler.AddJob(secondJob, false, true);
scheduler.AddJob(thirdJob, false, true);
//Console.ReadLine();
//scheduler.Shutdown();
//Console.WriteLine("Scheduler shutdown.");
//Console.WriteLine(history);
//Console.ReadLine();
}
}
public class SimpleJob1 : IJob
{
public virtual void Execute(IJobExecutionContext context)
{
JobKey jobKey = context.JobDetail.Key;
//log.InfoFormat("SimpleJob1 says: {0} executing at {1}", jobKey, DateTime.Now.ToString("r"));
Console.WriteLine("作业1: {0} executing at {1}", jobKey, DateTime.Now.ToString("r"));
System.Threading.Thread.Sleep(1000);
}
}
public class SimpleJob2 : IJob
{
public virtual void Execute(IJobExecutionContext context)
{
// This job simply prints out its job name and the
// date and time that it is running
JobKey jobKey = context.JobDetail.Key;
Console.WriteLine("作业2: {0} executing at {1}", jobKey, System.DateTime.Now.ToString("r"));
System.Threading.Thread.Sleep(2000);
}
}
public class SimpleJob3 : IJob
{
public virtual void Execute(IJobExecutionContext context)
{
// This job simply prints out its job name and the
// date and time that it is running
JobKey jobKey = context.JobDetail.Key;
Console.WriteLine("作业3: {0} executing at {1}", jobKey, System.DateTime.Now.ToString("r"));
}
}
}
注意:需要引用Quartz.dll,Common.Logging.dll,Common.Logging.Core.dll
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?