java - 未捕获异常对 Quartz 的 SimpleThreadPool 线程的影响

标签 java spring threadpool quartz-scheduler worker-thread

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

这是堆栈跟踪的示例:

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 的内容并记录错误。在任何情况下,工作线程都会返回到池中。

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

这就是说,Quartz documentation明确建议不要从作业中抛出任何异常,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.

关于java - 未捕获异常对 Quartz 的 SimpleThreadPool 线程的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11574777/

相关文章:

java - 如何从 JavaBean(域类)中的属性文件读取值?

java - 摆脱@Autowired(删除Spring依赖)

java - 结果集字段索引减一

JAVA JAI : between Jai Jars and Jai Setups, 我应该使用什么来获得更快、更有效的结果?

java - 监听 JFrame 的 Action

java - 使用 JSoup 解析 HTML(格式不正确)

java - 从另一个 jar 添加到 spring 接口(interface)实现

java - 用户定义线程和系统线程的区别(对于Sleep方法)

java - 并行化任务但保留输出中的输入顺序

clojure - 调用 shutdown-agents 后重新启动代理线程池