gpt4 book ai didi

c# - 使用 Quartz.NET 处理链式作业的失败

转载 作者:太空狗 更新时间:2023-10-30 01:34:11 24 4
gpt4 key购买 nike

我能够使用 Quartz.NET 安排 3 个链式作业。该策略运行良好:

var j1 = new TestJob1();
var j2 = new TestJob2();
var j3 = new TestJob3();

var jd1 = j1.Build();
var jd2 = j2.Build();
var jd3 = j3.Build();


var chain = new JobChainingJobListener("jobchain");
chain.AddJobChainLink(jd1.Key, jd2.Key);
chain.AddJobChainLink(jd2.Key, jd3.Key);


Scheduler.ListenerManager.AddJobListener(chain, GroupMatcher<JobKey>.AnyGroup());

Scheduler.ScheduleJob(jd1, j1.JobTrigger());
Scheduler.AddJob(jd2, true);
Scheduler.AddJob(jd3, true);


Scheduler.Start();

每个job的代码如下:

public class TestJob1 : BaseJob, IJob
{
public override ITrigger JobTrigger()
{
return TriggerBuilder.Create()
.WithSimpleSchedule(
ssb =>
ssb.WithInterval(new TimeSpan(0, 0, 0, 10)).RepeatForever().WithMisfireHandlingInstructionFireNow())
.Build();
}

public void Execute(IJobExecutionContext context)
{
Debug.WriteLine($"Running Job 1 at {DateTime.Now.ToString("O")}");
}
}

public class TestJob2 : BaseJob, IJob
{
public override ITrigger JobTrigger()
{
throw new System.NotImplementedException();
}

public void Execute(IJobExecutionContext context)
{
Debug.WriteLine($"Running Job 2 at {DateTime.Now.ToString("O")}");
throw new Exception("forced error");
}
}

public class TestJob3 : BaseJob, IJob
{
public override ITrigger JobTrigger()
{
throw new System.NotImplementedException();
}

public void Execute(IJobExecutionContext context)
{
Debug.WriteLine($"Running Job 3 at {DateTime.Now.ToString("O")}");
}
}

如果您看到,TestJob2 在运行时抛出异常。即使在这种情况下,TestJob3 也会被解雇。我的业务要求是,如果 TestJob2 失败,不应触发 TestJob3。请注意,实际上我不需要为 job2 和 job3 实现触发器,因为我在没有触发器的情况下将这些作业添加到调度程序。

这将如何完成?

提前致谢,马里奥

最佳答案

子类 JobChainingJobListener,并使用 JobChainingJobListenerFailOnError 代替 JobChainingJobListener:

/// <summary>
/// JobChainingJobListener that doesn't run subsequent jobs when one fails.
/// </summary>
public class JobChainingJobListenerFailOnError : JobChainingJobListener
{
public JobChainingJobListenerFailOnError(String name) : base(name) { }

public override void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
//Only call the base method if jobException is null. Otherwise, an
//error has occurred & we don't want to continue chaining
if (jobException == null)
base.JobWasExecuted(context, jobException);
}
}

关于c# - 使用 Quartz.NET 处理链式作业的失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31710780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com