Effects of uncaught exceptions on threads from Qua

2019-08-15 04:50发布

问题:

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?

This is a sample the stack trace:

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)**

回答1:

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.

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.

This said, Quartz documentation explicitly advices against throwing any exceptions out of your job except 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.