First of all, since a QuartzJobBean
is a Job
, any API call that will accept a Job
will accept a QuartzJobBean
, but not visa versa. So if you need a QuartzJobBean
because some API call expects you to pass it one, then there's your answer.
Otherwise, the answer depends on if you want to make use of (and be tied to) the functionality provided by QuartzJobBean
. If you look at the source code for that class, you'll see that the sole gain in subclassing QuartzJobBean
over implementing Job
, is that QuartzJobBean
performs this logic before passing control to your code:
try {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValues(context.getScheduler().getContext());
pvs.addPropertyValues(context.getMergedJobDataMap());
bw.setPropertyValues(pvs, true);
}
catch (SchedulerException ex) {
throw new JobExecutionException(ex);
}
So if you extend the QuartzJobBean
class and implement the executeInternal
method, this code runs before your code. If you implement the Job
class and the execute
method, it does not. That's the only difference between the two approaches in terms of what actually happens when your job runs.
So to answer your question, ask yourself "do I want to take advantage of the above code?". If the answer is Yes, then extend QuartzJobBean
to take advantage of that functionality. If you don't need this added functionality, don't want it, and/or don't want to be locked into the dependencies implied by the above code, then you should implement Job
to avoid this code and its dependencies. My personal approach would be to implement Job
unless I had some reason to extend QuartzJobBean
instead.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…