发现一个有趣的现象,一般job都会在执行前去初始化一次ac,而任务监视器SupervisorQueueJob不会,因此启动时初始化ac为null,SupervisorQueueJob会始终无法获取上下文,也即其所需的bean。使得队列任务得不到执行。
import org.quartz.JobExecutionContext;import org.springframework.beans.factory.annotation.Value;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.stereotype.Service;@SuppressWarnings("serial")@Service("supervisorQueueJob")public class SupervisorQueueJob extends AbstractJob { @Value("${quartz.job.autoStartup}") protected String jobStart; protected ThreadPoolTaskExecutor jobThreadPoolTaskExecutor; @Override protected void doExecute(JobExecutionContext context) { } @Override protected void afterExecute(JobExecutionContext context) { } @Override protected void beforeExecute(JobExecutionContext context) { } @Scheduled(cron = "*/10 * * * * ?") public void triggerSupervisorTask() { if (Boolean.parseBoolean(jobStart)) { LOGGER.info("监控线程执行一次" + System.currentTimeMillis()); jobThreadPoolTaskExecutor = (ThreadPoolTaskExecutor) ac.getBean("jobThreadPoolTaskExecutor"); jobThreadPoolTaskExecutor.submit((QueueSupervisorTask) SpringContextUtil.getBean("queueSupervisorTask")); } }}
原因:
<bean class="com.wind.buy.os.manager.utils.SpringContextUtil" />加载不能在SupervisorQueueJob 初始化之后(这个不言而喻)。
还有若是需要在Controller中手动触发定时任务,则对Controller包的扫描必须也在SupervisorQueueJob 之后,一般放在dispatch-servlet.xml中。
加载顺序可以在配置文件web.xml中指定。