Quartz 的 SimpleThreadPool 中未捕获异常对线程的影响 [英] Effects of uncaught exceptions on threads from Quartz's SimpleThreadPool

查看:48
本文介绍了Quartz 的 SimpleThreadPool 中未捕获异常对线程的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Spring 的集成 API 和 Quartz,对有未捕获异常的 cron 作业会有什么影响?由于 cronbean/worker 线程没有捕获异常,是否意味着该线程已死并且无法返回到 SimpleThreadPool?如果它死了并且没有回到池中,是否意味着 SimpleThreadPool 将需要创建新线程,如果说这种情况发生多次从而清空池?

Using Spring's integration api with Quartz, what will the effects be on cron jobs that have uncaught exceptions? Since the cronbean/worker thread did not catch the exception, will that mean the thread is dead and will not be able to go back to the SimpleThreadPool? If its dead and does not get back to the pool will that mean the SimpleThreadPool will need to create new threads, if say this happens multiple times thus emptying out the pool?

这是堆栈跟踪示例:

org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:276) - Invocation of method 'doCronJob' on target class [abc.package.ServiceImpl] failed
java.io.FileNotFoundException: http://www.website.com
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1457)
    at abc.package.ServiceImpl.doCronJob(ServiceImpl.java:453)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:283)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:272)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:208)
    **at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)**

推荐答案

JobRunShell 类是一个沙箱,在其中执行作业.它包含一个 catch (Throwable) 子句,用于捕获不是 JobExecutionException 的所有内容并记录错误.工作线程在任何情况下都会返回到池中.

The JobRunShell class is a sandbox in which job execution takes place. It contains a catch (Throwable) clause that catches everything that is not a JobExecutionException and logs the error. Worker threads are returned to pool in any case.

所以,答案是否定的,未处理的异常不会破坏 Quartz 线程池.触发器实现在 executionComplete 方法中的行为可能不同(例如取消调度或删除触发器).

So, the answer is no, unhandled exceptions do not break Quartz thread pools. Trigger implementations could behave differently (for example unschedule or delete the trigger) in the executionComplete method.

这就是说,Quartz 文档明确建议不要抛出任何异常除了 JobExecutionException 之外,你的工作:

This said, Quartz documentation explicitly advices against throwing any exceptions out of your job except JobExecutionException:

最后,我们需要通知您 Job.execute(..)方法.唯一类型的异常(包括 RuntimeExceptions)你被允许从 execute 方法抛出的是作业执行异常.因此,您通常应该将带有try-catch"块的 execute 方法的全部内容.你还应该花一些时间查看文档JobExecutionException,因为您的工作可以使用它来提供调度程序关于您希望如何处理异常的各种指令.

Finally, we need to inform you of a few details of the Job.execute(..) method. The only type of exception (including RuntimeExceptions) that you are allowed to throw from the execute method is the JobExecutionException. Because of this, you should generally wrap the entire contents of the execute method with a 'try-catch' block. You should also spend some time looking at the documentation for the JobExecutionException, as your job can use it to provide the scheduler various directives as to how you want the exception to be handled.

这篇关于Quartz 的 SimpleThreadPool 中未捕获异常对线程的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆