// Get Scheduler instance from SchedulerFactory. try { this.scheduler = createScheduler(schedulerFactory, this.schedulerName); populateSchedulerContext();
if (!this.jobFactorySet && !(this.scheduler instanceof RemoteScheduler)) { // Use AdaptableJobFactory as default for a local Scheduler, unless when // explicitly given a null value through the "jobFactory" bean property. this.jobFactory = new AdaptableJobFactory(); } if (this.jobFactory != null) { if (this.jobFactory instanceof SchedulerContextAware) { ((SchedulerContextAware) this.jobFactory).setSchedulerContext(this.scheduler.getContext()); } this.scheduler.setJobFactory(this.jobFactory); } }
// 后面的代码也省略掉了 }
我们可以看到中间这一句注释Use AdaptableJobFactory as default for a local Scheduler, unless when explicitly given a null value through the "jobFactory" bean property.意思就是这个SchedulerFactoryBean会默认将AdaptableJobFactory作为它的Job工厂。除非它的jobFactory属性被显式地赋上了一个非空的值。而默认情况下,我们在Spring的XML配置中都不会指定这个属性,所以默认就是这个AdaptableJobFactory了。那我们来看一看这个AdaptableJobFactory具体做了什么工作?
/** * Create an instance of the specified job class. * <p>Can be overridden to post-process the job instance. * @param bundle the TriggerFiredBundle from which the JobDetail * and other info relating to the trigger firing can be obtained * @return the job instance * @throws Exception if job instantiation failed */ protected Object createJobInstance(TriggerFiredBundle bundle)throws Exception { return bundle.getJobDetail().getJobClass().newInstance(); }
/** * Adapt the given job object to the Quartz Job interface. * <p>The default implementation supports straight Quartz Jobs * as well as Runnables, which get wrapped in a DelegatingJob. * @param jobObject the original instance of the specified job class * @return the adapted Quartz Job instance * @throws Exception if the given job could not be adapted * @see DelegatingJob */ protected Job adaptJob(Object jobObject)throws Exception { if (jobObject instanceof Job) { return (Job) jobObject; } elseif (jobObject instanceof Runnable) { returnnew DelegatingJob((Runnable) jobObject); } else { thrownew IllegalArgumentException("Unable to execute job class [" + jobObject.getClass().getName() + "]: only [org.quartz.Job] and [java.lang.Runnable] supported."); } }