将 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/