如何使用 Quartz 调度程序维护作业历史记录
我想维护由 Quartz 调度程序调度的作业的历史记录,其中包含以下属性:“开始时间”、“结束时间”、“成功”、“错误”。
有两个接口可用于此:ITriggerListener
和 IJobListener
(我使用接口的 C# 命名约定,因为我使用的是 Quartz.NET,但同样的问题可能是要求 Java 版本)。
IJobListener
有一个 JobToBeExecuted
和一个 JobWasExecuted
方法。后者提供了一个JobExecutionException,以便您知道何时出现问题。但是,无法关联 JobToBeExecuted
和 JobWasExecuted
。假设我的工作运行十分钟。我从 t0
和 t0+2
开始(因此它们重叠)。我收到两次对 JobToBeExecuted
的调用,并将两个开始时间插入到我的历史记录表中。当两个作业在 t1
和 t1+2
完成时,我收到两次对 JobWasExecuted
的调用。我如何知道在每次调用中要更新哪些数据库记录(以存储结束时间及其相应的开始时间)?
ITriggerListener
还有另一个问题。当作业失败时,无法在 TriggerComplete 方法内获取任何错误。
我如何获得所需的行为?
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'.
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
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现此目的的方法是在
JobToBeExecuted
中生成一个标识符,将其存储在JobExecutionContext
中,并从中的
。JobExecutionContext
中再次检索它作业已执行The way to do this is to generate an identifier in
JobToBeExecuted
, store it in theJobExecutionContext
and retrieve it again from theJobExecutionContext
inJobWasExecuted
.调度程序必须维护一个密钥,使其能够关联每个历史记录条目。必须在作业启动时创建某种唯一的作业 ID 才能完成此任务。
你没有提到这样的事情,所以我认为值得提出建议。
更新:当创建作业时,我会在数据库中插入一条记录并取回主键(可能是 GUID)。我会用它作为钥匙。
The scheduler has to maintain a key that lets it correlate each history entry. There must be a unique job ID of some kind that's created when a job kicks off to be able to accomplish this.
You don't mention such a thing, so I thought it was worth a suggestion.
UPDATE: I'd INSERT a record into the database when a job is created and get back the primary key (maybe a GUID). I'd use that as the key.
如果您愿意在最后更新数据库,则可以从
IJobExecutionContext
获取作业名称和运行时间:If you're happy to just update the database at the end, you can get the job name and run time from the
IJobExecutionContext
: