在 Quartz.net 中处理 JobExecutionException
可能是一个愚蠢的问题...但无论如何...
我已经设置了quartz,并且可以安排作业,并且我可以确认作业(实现 IJob 接口)正在工作。
查看网站上的文档(教程的第 3 课):
允许您从执行方法抛出的唯一异常类型是
JobExecutionException
。
我希望当发生我没有明确处理的异常时,它应该抛出 JobExecutionException,以便我可以将其记录在“父”应用程序中。我已将代码包装在 try catch 中,并抛出了 JobExecutionException,但现在在哪里处理它?
我不会在任何地方调用由 Quartz 处理的执行方法(在单独的线程上)。那么,当发生该错误时,我该如何处理。我真的不想吞下作业中的错误
Probably a stupid question... but here goes anyway...
I have set up quartz, and can schedule jobs, and I can confirm that jobs (implementing the IJob interface) are working.
Looking at the documentation on the site, (Lesson 3 of the tutorial):
The only type of exception that you are allowed to throw from the execute method is
JobExecutionException
.
I would like that when an exception occurs that I haven't explicitly handled, it should throw a JobExecutionException, so that I can log it in the 'parent' application. I have wrapped my code in a try catch, and have thrown the JobExecutionException, but now where to handle it?
I don't call the execute method anywhere, that is handled by Quartz (on a separate thread). So, how do I handle that error when it occurs. I don't really want to swallow the error in the Job
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过使用基类捕获所有异常解决了这个问题:
您的 Job 类应该如下所示:
I solved this problem by using a base class to catch all the exceptions:
Your Job class should like this:
通常,您将按如下方式设置作业的执行方法:
此时,调度程序本身会将异常记录到其记录的任何位置(基于配置)。
Typically you would set up the execute method of your job as follows:
At this point the scheduler itself will log the exception to wherever it is logging (based on the configuration).
正如已经提到的,在全局级别“检测”JobExecutionException 的正确方法是实现并注册一个 IJobListener 并检查 JobWasExecuted() 方法中的 JobExecutionException 参数是否为 != null。
然而,我遇到的问题(从OP的附加评论来看,他也面临着这个问题)是Quartz没有处理JobExecutionException(它应该),这导致未处理的异常杀死了应用程序。
到目前为止,我使用的是 Quartz.NET 2.0.1 版本 (.NET3.5) 包中的预编译 DLL。为了找出问题的根源,我引用了 Quartz 项目/源代码,令我惊讶的是它突然工作了?!
有趣的是,这是执行 IJob 并处理 JobExecutionException 的 Quartz 库代码:
接下来是直接引用我新编译的 DLL,这也有效。遗憾的是,我无法告诉你为什么这有效,而且我目前没有时间进一步了解它,但这也许对某人有帮助。也许其他人可以证实这一点,甚至做出解释。可能与不同的目标平台(x86/64bit)有关?
As already mentioned, the correct way to "detect" JobExecutionException's on a global level is to implement and register an IJobListener and check if the JobExecutionException parameter in the JobWasExecuted() method is != null.
However, the problem I had (and judging from the additional comment of the OP, he faced this too) was that Quartz did not handle the JobExecutionException (as it should) which resulted in an unhandled exception killing the application.
So far, I was using the precompiled DLL from the Quartz.NET 2.0.1 release (.NET3.5) package. To get to the bottom of the problem, i referenced the Quartz project/sourcecode and to my astonishment it was suddenly working?!
As a point of interest, this is the Quartz library code that executes the IJob and handles the JobExecutionException:
The next thing was to reference my freshly compiled DLL direcly and this was working as well. Sadly i can't tell you why this works and i currently don't have any time to get into it any further, but maybe this helps someone. Maybe some else can confirm this and even contribute an explanation. It may have something to do with different target platforms (x86/64bit)?