简体   繁体   English

当我们使用@EnableScheduling 时,我们是否应该始终创建一个名为“taskExecutor”的 TaskExecutor bean

[英]Should we always create a TaskExecutor bean named 'taskExecutor' when we use @EnableScheduling

I have a configuration class without explicitly bean creation method in the class:我有一个配置 class 没有在 class 中明确创建 bean 方法:

@Configuration
@EnableAsync
@EnableScheduling
public class PollingAgent {

    @Async
    @Scheduled(fixedRate=INTERVAL_RATE, initialDelay = 10000) //setting an initial delay to let the beans get loaded
    void checkAvailableAndStartJob() {
        if(m_messageOnDemand) {
            try {
                LOGGER.info("check job messages and start job if possible");
                jobIdOnDemandSubscriber.StartJobIfMessageAvailable(m_applicationContext);
            } catch (Exception e) {
                LOGGER.info("Scheduler run failed: " + e.getMessage());
            }
        }
    }
}

But I got this log when boot run: scheduling-1: INFO org.springframework.scheduling.annotation.AnnotationAsyncExecutionInterceptor - More than one TaskExecutor bean found within the context, and none is named 'taskExecutor'.但是我在启动运行时得到了这个日志: scheduling-1: INFO org.springframework.scheduling.annotation.AnnotationAsyncExecutionInterceptor - 在上下文中发现了多个 TaskExecutor bean,没有一个被命名为“taskExecutor”。 Mark one of them as primary or name it 'taskExecutor' (possibly as an alias) in order to use it for async processing: [HappyMondayExecutor, taskScheduler]\n","stream":"stdout","time":"2022-08-05T20:33:41.727269489Z"}将其中一个标记为主要或将其命名为“taskExecutor”(可能作为别名),以便将其用于异步处理:[HappyMondayExecutor, taskScheduler]\n","stream":"stdout","time":"2022 -08-05T20:33:41.727269489Z"}

Should I add TaskExecutor bean named 'taskExecutor' in PollingAgent ?我应该在PollingAgent中添加名为“taskExecutor”的 TaskExecutor bean 吗? :

@Bean
public Executor taskExecutor() {
    return Executors.newSingleThreadScheduledExecutor();
}

Why not the spring framework create a bean named 'taskExecutor' automatically when using @EnableScheduling?为什么 spring 框架在使用 @EnableScheduling 时不自动创建一个名为“taskExecutor”的 bean?

Well, the info message is happening because you have more than one TaskExecutor bean declared in your application context.好吧,信息消息正在发生,因为您在应用程序上下文中声明了多个TaskExecutor bean。

From @EnableAsync javadoc:来自@EnableAsync javadoc:

By default, Spring will be searching for an associated thread pool definition: either a unique TaskExecutor bean in the context, or an Executor bean named "taskExecutor" otherwise.默认情况下,Spring 将搜索关联的线程池定义:要么是上下文中唯一的 TaskExecutor bean,要么是名为“taskExecutor”的 Executor bean。 If neither of the two is resolvable, a SimpleAsyncTaskExecutor will be used to process async method invocations.如果两者都无法解析,则将使用 SimpleAsyncTaskExecutor 来处理异步方法调用。 Besides, annotated methods having a void return type cannot transmit any exception back to the caller.此外,具有 void 返回类型的带注释的方法不能将任何异常传输回调用者。 By default, such uncaught exceptions are only logged.默认情况下,仅记录此类未捕获的异常。

The log message is just an info saying "Hey, I've found two TaskExecutor declarations. Are you sure none of these was intended to be the executor for your @Async tasks?"日志消息只是一条信息,上面写着“嘿,我发现了两个TaskExecutor声明。你确定这些都不是你的@Async任务的执行者吗?”

Anyway, as said in the quote above, a SimpleAsyncTaskExecutor bean have been designated for your @Async annotated methods executions.无论如何,正如上面引用中所说,已为您的@Async注释方法执行指定了一个SimpleAsyncTaskExecutor bean。

Now for your question:现在回答你的问题:

Why not the spring framework create a bean named 'taskExecutor' automatically when using @EnableScheduling?为什么 spring 框架在使用 @EnableScheduling 时不自动创建一个名为“taskExecutor”的 bean?

Well, the @Scheduled annotated methods relies on a Scheduler.好吧, @Scheduled注释方法依赖于调度程序。 Now, a quote from @EnableScheduling javadoc :现在,来自@EnableScheduling javadoc的引用:

By default, Spring will search for an associated scheduler definition: either a unique TaskScheduler bean in the context, or a TaskScheduler bean named "taskScheduler" otherwise;默认情况下,Spring 将搜索关联的调度程序定义:要么是上下文中唯一的 TaskScheduler bean,要么是名为“taskScheduler”的 TaskScheduler bean; the same lookup will also be performed for a ScheduledExecutorService bean.还将对 ScheduledExecutorService bean 执行相同的查找。 If neither of the two is resolvable, a local single-threaded default scheduler will be created and used within the registrar.如果两者都不可解析,则将在注册器中创建并使用本地单线程默认调度程序。

Spring does not create a bean called "taskExecutor". Spring 不会创建名为“taskExecutor”的 bean。 It just creates, as said above, a local single-threaded default scheduler.如上所述,它只是创建了一个本地单线程默认调度程序。

Without any task executor the below code will run.没有任何任务执行器,下面的代码将运行。 Please check which version of spring boot you are using.请检查您使用的是哪个版本的 spring 引导。

@Configuration
@EnableAsync
@EnableScheduling
public class PollingAgent {
    Logger log = LoggerFactory.getLogger(PollingAgent.class);

    @Async
    @Scheduled(fixedRate=1000, initialDelay = 10000) //setting an initial delay to let the beans get loaded
    void checkAvailableAndStartJob() {
        log.info("started");
    }

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM