没有ScheduledExecutorService类型的限定bean的TaskScheduler

时间:2017-03-14 14:49:42

标签: java spring spring-scheduled

这是调度配置

@Configuration
@EnableScheduling
public class RWInventorySchedule {

protected org.slf4j.Logger log = LoggerFactory.getLogger(RWInventorySchedule.class);

@PersistenceContext
private EntityManager entityManager;


   @Bean
   public RWInventoryProcessor constructInventoryProcessor() {
       log.debug("RWInventorySchedule constructs InventoryProcessor, entityManager : {} " , entityManager);
       return new RWInventoryProcessor(entityManager);
    }
}

库存处理器如下

public class RWInventoryProcessor  {
 ...
 @Scheduled(fixedRate = 5000,initialDelay = 3000)
 @Transactional
 public void runProcess() {
   ...
 }
}

执行期间,在调试日志中收到以下错误

  

DEBUG   org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor    - 找不到默认的TaskScheduler bean org.springframework.beans.factory.NoSuchBeanDefinitionException:否   属于' org.springframework.scheduling.TaskScheduler'的限定bean   可得到   

  ...
  DEBUG org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor    - 找不到默认的ScheduledExecutorService bean org.springframework.beans.factory.NoSuchBeanDefinitionException:否   合格的bean类型   ' java.util.concurrent.ScheduledExecutorService'可用

我错过了什么

2 个答案:

答案 0 :(得分:14)

如果您正在使用Java配置,则需要为要使用的调度程序类型定义@Bean。 Spring没有这个默认bean。例如

@Bean
public TaskScheduler taskScheduler() {
    return new ConcurrentTaskScheduler(); //single threaded by default
}

答案 1 :(得分:0)

我使用了这个表单我的xml Spring配置:

<task:annotation-driven  executor="executor" />

<task:scheduler id="scheduler" pool-size="10"/>

<task:executor id="executor" pool-size="7"/>

我的组件处理器是:

@Component
public class QueueConsumer {

    @Autowired
    ProcessQueue queue;

    @Autowired
    TaskProcessor processor;

    @Autowired
    TaskResultRepository resultRepository;

    @Scheduled(fixedDelay = 15000)
    public void runJob() {
        ProcessTask task = queue.poll();
        ...
    }
}

您需要等同于<task:scheduler id="scheduler" pool-size="10"/>

的注释