如何使用Quartz调度程序维护作业历史记录

Ron*_*erg 9 c# java task quartz-scheduler

我想保留由Quartz调度程序安排的作业历史,其中包含以下属性:"开始时间","结束时间","成功","错误".

有两个可用的接口:ITriggerListenerIJobListener(我正在使用接口的C#命名约定,因为我使用的是Quartz.NET,但可能会要求Java版本提出相同的问题).

IJobListenerJobToBeExecuted一个JobWasExecuted方法.后者提供了一个JobExecutionException让你知道什么时候出错的地方.但是,没有办法关联JobToBeExecutedJobWasExecuted.假设我的工作运行了十分钟.我在启动t0t0+2(让他们重叠).我接到两个电话,JobToBeExecuted并在历史表中插入两个开始时间.当两个工作完成时t1,t1+2我接到两个电话JobWasExecuted.我如何知道每次调用中要更新的数据库记录(存储结束时间及其相应的开始时间)?

ITriggerListener有另一个问题.TriggerComplete当作业失败时,无法在方法内部获得任何错误.

我如何获得所需的行为?

Ron*_*erg 12

要做到这一点的方法是在产生一个标识符JobToBeExecuted,它存储在JobExecutionContext从再次检索它JobExecutionContextJobWasExecuted.

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, ...);
}
Run Code Online (Sandbox Code Playgroud)