如何使用Quartz调度维护作业历史 [英] How to maintain a job history using Quartz scheduler
问题描述
我想保持这是由包含以下属性Quartz调度调度作业的历史记录:开始时间,结束时间,成功,错误
I'd like to maintain a history of jobs that were scheduled by a Quartz scheduler containing the following properties: 'start time', 'end time', 'success', 'error'.
有可用于该两个接口: ITriggerListener
和 IJobListener
(我使用对于接口C#的命名规则,因为我使用Quartz.NET但同样的问题可以问Java版本)。
There are two interfaces available for this: ITriggerListener
and IJobListener
(I'm using the C# naming convention for interfaces because I'm using Quartz.NET but the same question could be asked for the Java version).
IJobListener
有一个 JobToBeExecuted
和 JobWasExecuted
方法。后者提供了 JobExecutionException
,让你知道什么时候出事了。但是,有没有办法来关联 JobToBeExecuted
和 JobWasExecuted
。假设我的工作十分钟运行。我在 T0启动
和 0 + 2
(让他们重叠)。我得到两次调用 JobToBeExecuted
键,插入两个开始时间到我的历史记录表。当两个作业完成的 T1
和 T1 + 2
我得到两次调用 JobWasExecuted
。我怎么知道是什么数据库记录在每次调用更新(存储的结束时间与它相应的起始时间)?
IJobListener
has a JobToBeExecuted
and a JobWasExecuted
method. The latter provides a JobExecutionException
so that you know when something went wrong. However, there is no way to correlate JobToBeExecuted
and JobWasExecuted
. Suppose my job runs for ten minutes. I start it at t0
and t0+2
(so they overlap). I get two calls to JobToBeExecuted
and insert two start times into my history table. When both jobs finish at t1
and t1+2
I get two calls to JobWasExecuted
. How do I know what database record to update in each call (to store an end time with its corresponding start time)?
ITriggerListener
还有另一个问题。有没有办法让里面的 TriggerComplete
方法,当一个作业失败。
ITriggerListener
has another problem. There is no way to get any errors inside the TriggerComplete
method when a job failed.
我如何得到任何错误所需的行为?
How do I get the desired behavior?
推荐答案
要做到这一点的方法是产生一个标识符 JobToBeExecuted
,并将其存储在 JobExecutionContext
和从 JobExecutionContext
在 JobWasExecuted再次检索
。
The way to do this is to generate an identifier in JobToBeExecuted
, store it in the JobExecutionContext
and retrieve it again from the JobExecutionContext
in JobWasExecuted
.
public void JobToBeExecuted(JobExecutionContext context)
{
// Insert history record and retrieve primary key of inserted record.
long historyId = InsertHistoryRecord(...);
context.Put("HistoryIdKey", historyId);
}
public void JobWasExecuted(JobExecutionContext context,
JobExecutionException jobException)
{
// Retrieve history id from context and update history record.
long historyId = (long) context.Get("HistoryIdKey");
UpdateHistoryRecord(historyId, ...);
}
这篇关于如何使用Quartz调度维护作业历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!