spring boot添加quartz定时器报错但是定时器正常运行
[DEBUG] 2017-11-29 11:28:13,533 org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor - Could not find default TaskScheduler bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.scheduling.TaskScheduler' available
检查之后发现
,Spring的定时任务调度器会尝试获取一个注册过的 task scheduler来做任务调度,它会尝试通过BeanFactory.getBean的方法来获取一个注册过的scheduler bean,获取的步骤如下:
1.尝试从配置中找到一个TaskScheduler Bean
2.寻找ScheduledExecutorService Bean
3.使用默认的scheduler
前两步,如果找不到的话,就会以debug的方式抛出异常,分别是:
logger.debug("Could not find default TaskScheduler bean", ex);
logger.debug("Could not find default ScheduledExecutorService bean", ex);
所以,日志中打印出来的两个异常,根本不是什么错误信息,也不会影响定时器的使用,只不过是spring的自己打印的一些信息罢了,不过没搞明白,为什么非要用异常的方式打出来,估计是为了看这清晰点吧。也或者,这里面有一些重要的信息需要提示开发者。具体是什么原因,只能有机会进一步再去了解了。
下面贴上我的定时器代码。仅供记录参考:
package com.tt.pwp.report.quartz; import com.tt.pwp.report.ws.client.request.PlatFormConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * Created by Administrator on 2017/11/24 0024. */ @Component @Configurable @EnableScheduling public class ReportDataQuartz { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private PlatFormConfig ymlConfig; @Scheduled(fixedRate = 500000) // 每分5m行一次 public void doReportOneDay() throws Exception { System.out.println("执行调度任务:"+new Date()); } // public void doReportTwoDays() throws Exception { // System.out.println("执行调度任务:"+new Date()); // } // public void doReportThereDays() throws Exception { // System.out.println("执行调度任务:"+new Date()); // } // public void doReportFourDays() throws Exception { // System.out.println("执行调度任务:"+new Date()); // } // public void doReportFiveDays() throws Exception { // System.out.println("执行调度任务:"+new Date()); // } // public void doReportSixDays() throws Exception { // System.out.println("执行调度任务:"+new Date()); // } // public void doReportOneWeek() throws Exception { // System.out.println("执行调度任务:"+new Date()); // } // public void doReportOneMonth() throws Exception { // System.out.println("执行调度任务:"+new Date()); // } }