Skip to content

Regression: AsyncAnnotationBeanPostProcessor fails on startup when encountering ambiguous TaskExecutor beans [SPR-13720] #18293

Closed

Description

@spring-projects-issues

Piotr Leśniak opened SPR-13720 and commented

After upgrading spring-core from 4.1.5.RELEASE to 4.2.3.RELEASE context of our applications failed to start. The problem is with too many beans qualifying to 'taskExecutor' :

java.lang.IllegalStateException: More than one TaskExecutor bean exists within the context, and none is named 'taskExecutor'. Mark one of them as primary or name it 'taskExecutor' (possibly as an alias); or specify the AsyncConfigurer interface and implement getAsyncExecutor() accordingly ...
Caused by: 
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.core.task.TaskExecutor] is defined: expected single matching bean but found 4: clientOutboundChannelExecutor,messageBrokerTaskScheduler,clientInboundChannelExecutor,brokerChannelExecutor

It is related to AsyncAnnotationBeanPostProcessor which tries to instantiate taskExecutor for @Async annotated methods in setBeanFactory method.

The fix is easy, it requires only to create new bean named 'taskExecutor' or mark one of existing with @Primary.

However this bug is a pain for following reasons:

  1. Neither of the existing task executors in context (clientOutboundChannelExecutor,messageBrokerTaskScheduler,clientInboundChannelExecutor,brokerChannelExecutor) is created by us - all of them comes from some others spring libraries (I think messaging and websockets?)
  2. We don't use @Async annotation and we don't have @EnableAsync anywhere in our code.

Is there a way to disable AsyncAnnotationBeanPostProcessor from registering and creating taskExecutor?

Also, a logic in instantiating taskExecutor is a bit odd to me - maybe throwing exception when there are many Beans implementing TaskExecutor and no bean with name "taskExecutor" is not a good behaviour?


Affects: 4.2 GA

Issue Links:

Referenced from: commits 2a3bf69

Activity

  1. spring-projects-issues commented on Nov 25, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    Can you tell us a bit more about your project? The post processor won't be invoked unless @EnableAsync is added to some configuration class.

  2. spring-projects-issues commented on Nov 25, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    Juergen Hoeller the previous behaviour was to create the advisor with a null executor which in turn would create a default instance. Is there a reason why you took the decision to throw an exception instead. Maybe a warning would be better?

  3. spring-projects-issues commented on Nov 25, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Piotr Leśniak commented

    We don't have @EnableAsync implicitly declared in any of out @Configuration classes. Could it come transitively from some other spring project?

    We don't use websockets. We have some @Scheduled methods.

    Here is complete list of spring projects with versions which we include:

    [INFO] |  +- org.springframework:spring-core:jar:4.2.3.RELEASE:compile
    [INFO] |  +- org.springframework:spring-beans:jar:4.2.3.RELEASE:compile
    [INFO] |  \- org.springframework:spring-aop:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework:spring-web:jar:4.2.3.RELEASE:compile
    [INFO] |  \- org.springframework:spring-context:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework:spring-webmvc:jar:4.2.3.RELEASE:compile
    [INFO] |  \- org.springframework:spring-expression:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework:spring-tx:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework:spring-jdbc:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework:spring-orm:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework:spring-context-support:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework:spring-aspects:jar:4.2.3.RELEASE:compile
    [INFO] +- org.springframework.data:spring-data-commons:jar:1.9.2.RELEASE:compile
    

    Class that bootstraps our context extends DelegatingWebMvcConfiguration (but we don't touch configureAsyncSupport method) and web app initializer class implements WebApplicationInitializer.

    I've searched our project and some projects that we depend on looking for @EnableAsync and I've found nothing.

  4. spring-projects-issues commented on Nov 25, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    AsyncAnnotationBeanPostProcessor is supposed to leniently handle such a scenario, simply falling back to an internal default executor then - which might not even apply if no @Async is being used in the first place. In other words, it should be able to exist next to @Scheduled processing within the same application even if no @Async execution setup is available.

    I've changed the exception to an info log message (without exception), just indicating that async annotation processing will use an internal default executor due to no corresponding bean found.

    Juergen

  5. spring-projects-issues commented on Nov 25, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This is available in the latest 4.2.4.BUILD-SNAPSHOT in the meantime. Please give it a try and let me know whether it works for you...

    Juergen

  6. added
    type: bug
    in: core
    on Jan 11, 2019
  7. added this to the 4.2.4 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions