如果作业异常失败,请在15分钟后重新启动quartz.net触发器 [英] Refire quartz.net trigger after 15 minutes if job fails with exception
问题描述
如果作业抛出异常,我已经搜索了有关在一定的时间后如何重新触发作业的答案。我看不到任何简单的方法。
I have searched for an answer on how to retrigger a job after a ceratin amount of time, if the job throws an exception. I cannot see any simple way of doing this.
如果我这样设置触发器:
if I set my trigger up like this:
JobDetail job = new JobDetail("Download catalog", null, typeof(MyJob));
job .Durable = true;
Trigger trigger= TriggerUtils.MakeDailyTrigger(12, 0);
trigger.StartTimeUtc = DateTime.UtcNow;
trigger.Name = "trigger name";
scheduler.ScheduleJob(job , trigger);
MyJob看起来像这样:
And MyJob look like this:
public class MyJob : IJob
{
public void Execute(JobExecutionContext context)
{
var service = new service();
try
{
service.Download();
}
catch (Exception)
{
throw;
}
}
}
怎么办如果service.Download()调用引发某种异常,我会在15分钟后触发触发器,然后重新触发。
how do I make the trigger to refire/retrigger after there is gone 15 minutes if the service.Download() call throws some sort of Exception?
推荐答案
我认为您唯一的选择是捕获错误并告诉Quartz.net立即重新发射:
I think the only option you have is to trap the error and tell Quartz.net to refire immediately:
public class MyJob : IJob
{
public void Execute(JobExecutionContext context)
{
var service = new service();
try
{
service.Download();
}
catch (Exception ex)
{
JobExecutionException qe = new JobExecutionException(ex);
qe.RefireImmediately = true; // this job will refire immediately
throw qe;
}
}
}
您可以找到一些信息< a href = http://quartznet.sourceforge.net/apidoc/1.0/html/topic415.html rel = noreferrer>此处和此处。
You can find some info here and here.
更新:
我做了一些测试,看来您可以在正在执行的作业中安排新的触发器。
您可以尝试执行以下操作:
I did some tests and it seems that you can schedule a new trigger inside an executing job.
You can try something like this:
public class MyJob : IJob
{
public void Execute(JobExecutionContext context)
{
var service = new service();
try
{
service.Download();
}
catch (Exception ex)
{
JobExecutionException qe = new JobExecutionException(ex);
// qe.RefireImmediately = true; // this job will refire immediately
// throw qe;
OnErrorScheduleJob(context);
}
}
private void OnErrorScheduleJob(JobExecutionContext context)
{
var jobOnError = context.Scheduler.GetJobDetail("ONERRORJOB", "ERROR");
if (jobOnError == null)
{
JobDetail job = new JobDetail("ONERRORJOB", "ERROR", typeof(MyJob));
job.Durable = false;
job.Volatile = false;
job.RequestsRecovery = false;
SimpleTrigger trigger = new SimpleTrigger("ONERRORTRIGGER",
"ERROR",
DateTime.UtcNow.AddMinutes(15),
null,
1,
TimeSpan.FromMinutes(100));
context.Scheduler.ScheduleJob(job, trigger);
}
}
}
这篇关于如果作业异常失败,请在15分钟后重新启动quartz.net触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!