Java 没有 ScheduledExecutorService 类型的合格 bean | 任务调度器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42789209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:24:47  来源:igfitidea点击:

No qualifying bean of type ScheduledExecutorService | TaskScheduler

javaspringspring-scheduled

提问by vels4j

Here is the Scheduling Configuration

这是调度配置

@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);
    }
}

Inventory Processor is the following

库存处理器如下

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

During execution, getting the following errors in debug log

在执行过程中,在调试日志中得到以下错误

DEBUG 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

...
DEBUG org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor - Could not find default ScheduledExecutorService bean org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.concurrent.ScheduledExecutorService' available

调试 org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor - 找不到默认的 TaskScheduler bean org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有可用的“org.springframework.scheduling.TaskScheduler”类型的合格 bean

...
调试 org.springframework. schedule.annotation.ScheduledAnnotationBeanPostProcessor - 找不到默认的 ScheduledExecutorService bean org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有可用的“java.util.concurrent.ScheduledExecutorService”类型的合格 bean

Am I missing anything

我错过了什么吗

采纳答案by Adam

If you're using Java configuration you need an @Bean definition for the type of scheduler you wish to use. Spring does not have a default bean for this. For example

如果您使用 Java 配置,则需要为您希望使用的调度程序类型定义 @Bean。Spring 没有为此提供默认 bean。例如

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

回答by rvillablanca

I have used this form my xml Spring Configuration:

我在我的 xml Spring 配置中使用了这种形式:

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

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

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

My Component processor is:

我的组件处理器是:

@Component
public class QueueConsumer {

    @Autowired
    ProcessQueue queue;

    @Autowired
    TaskProcessor processor;

    @Autowired
    TaskResultRepository resultRepository;

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

You need the annotation equivalent to <task:scheduler id="scheduler" pool-size="10"/>

您需要等效于的注释 <task:scheduler id="scheduler" pool-size="10"/>