Skip to content

Async() is not invoked in a separate thread after upgrading from 1.2.6 to 1.3.3 #5366

Closed

Description

@WeipingGuo

Not sure what changed. We have a simple setup. The Kafka task used to be started in its own executor, but after upgrading to 1.3.3 it always runs in the main thread. Anything I missed? Thanks in advance for your suggestions and helps.

`
@configuration
@EnableAutoConfiguration
@EnableAsync
@EnableCaching
@EnableSwagger
public class Application implements CommandLineRunner {

@Autowired
private KafkaConsumerTask kafkaTask;

public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
}

@Override
public void run(String... args) throws Exception {
    kafkaTask.start();
}

}

@component
public class KafkaConsumerTask {

@Async("kafkaTaskExecutor")
public void start() {
    log.info("starting consumer for topic: " + topic);
    ...
}

@Bean
public ThreadPoolTaskExecutor kafkaTaskExecutor() {
    ThreadPoolTaskExecutor bean = new ThreadPoolTaskExecutor();
    bean.setCorePoolSize(numberThreads);
    bean.setMaxPoolSize(numberThreads * 2);
    bean.setThreadPriority(priority);
    bean.setWaitForTasksToCompleteOnShutdown(true);
    return bean;
}       

}
`

Activity

  1. added
    status: waiting-for-triage
    on Mar 8, 2016
  2. wilkinsona commented on Mar 8, 2016

    @wilkinsona
    Member

    Can you please provide a complete sample app that reproduces the problem?

  3. added
    status: waiting-for-feedback
    and removed
    status: waiting-for-triage
    on Mar 8, 2016
  4. WeipingGuo commented on Mar 8, 2016

    @WeipingGuo
    Author

    Thanks. I created a sample project: https://github.com/WeipingGuo/boot-async

    I ran the same code using different spring boot versions. Appears that the @async is broken on 1.3.0 as it works as expected on 1.2.8

    1.2.8
    2016-03-08 15:07:36.613 INFO 5452 --- [aTaskExecutor-1] wguo.test.KafkaConsumerTask : starting consumer for topic testing

    1.3.0
    2016-03-08 18:14:20.464 INFO 7680 --- [ main] wguo.test.KafkaConsumerTask : starting consumer for topic testing

    1.3.3
    2016-03-08 15:08:49.341 INFO 5908 --- [ main] wguo.test.KafkaConsumerTask : starting consumer for topic testing

  5. added
    status: feedback-provided
    and removed
    status: waiting-for-feedback
    on Mar 8, 2016
  6. snicoll commented on Mar 9, 2016

    @snicoll
    Member

    Two things:

    • You are creating a @Bean from a @Component class. That should be specified in a @Configuration class
    • You are injecting the service using field injection in the application class. Because it has to be instantiated quite early, I guess the framework is unable to inject the bean with the async proxy around it

    If I move the task executor to the application and I move the CommandLineRunner to an inner class of the app it works.

    Having said that, it's sill weird that it works in 1.2.x

  7. wilkinsona commented on Mar 9, 2016

    @wilkinsona
    Member

    It also stops working with 1.2.x if you upgrade Spring Framework to 4.2.5.RELEASE

  8. wilkinsona commented on Mar 9, 2016

    @wilkinsona
    Member

    Running with 4.2.5.RELEASE triggers these log messages:

    2016-03-09 09:52:00.916  INFO 48097 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'kafkaConsumerTask' of type [class wguo.test.KafkaConsumerTask] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2016-03-09 09:52:00.917  INFO 48097 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'application' of type [class wguo.test.Application$$EnhancerBySpringCGLIB$$7d48eac9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2016-03-09 09:52:00.929  INFO 48097 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService  'kafkaTaskExecutor'
    2016-03-09 09:52:00.931  INFO 48097 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'kafkaTaskExecutor' of type [class org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    

    Something's changed in Spring Framework 4.2.x that means that something is triggering very early instantiation of the KafkaTaskExecutor bean, so early that it isn't eligible for bean post-processing which prevents the proxy for async method execution from being created.

  9. wilkinsona commented on Mar 9, 2016

    @wilkinsona
    Member

    I can recreate this with pure Spring Framework so this isn't a problem with Spring Boot itself. I have opened SPR-14030 to track the problem.

  10. removed
    status: feedback-provided
    on Mar 9, 2016
  11. WeipingGuo commented on Mar 9, 2016

    @WeipingGuo
    Author

    Thanks for looking into it. @snicoll You are right. My bean creation was wrong, somehow it worked before.

    I moved the ThreadPoolTaskExecutor to its own class with @configuration. Now it works as charm.

    I guess it is not bug in Spring rather than my programming error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions