Spring @Async method call inside @Scheduled method

倾然丶 夕夏残阳落幕 提交于 2019-12-08 17:26:27

问题


I am using Spring boot with @EnableScheduling and @EnableAsync.

I have a method which is annotated with @Scheduled. I have a few more methods, which are annotated with @Async.

Now I am calling these @Async methods in the @Scheduled method and printing out the name of the current thread in the async methods. What I see is they all have same thread name, which in fact is the thread that is running the @Scheduled method.

I don't see asynchronous method execution. What is wrong here?

Here is my application boot class

@SpringBootApplication
@EnableScheduling
@EnableAsync
public class ApplicationBoot {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationBoot.class, args);
    }
}

Here is my scheduler class

@Component
public class TaskScheduler {
    private static final Logger logger = Logger.getLogger(TaskScheduler.class);

    @Scheduled(fixedDelay = 10000)
    public void ScheduledMethod() {
        methodOne();
        methodTwo();
        methodThree();
    }

    @Async
    private void methodOne() {
        logger.info("Method one called  by Thread : " + Thread.currentThread().getName() + "  at " + new Date());
    }

    @Async
    private void methodTwo() {
        logger.info("Method two called  by Thread : " + Thread.currentThread().getName() + "  at " + new Date());
    }

    @Async
    private void methodThree() {
        logger.info("Method three called  by Thread : " + Thread.currentThread().getName() + "  at " + new Date());
    }
}

Output

Method one called by Thread : pool-1-thread-1 at Tue Apr 04 16:32:27 IST 2017

Method two called by Thread : pool-1-thread-1 at Tue Apr 04 16:32:27 IST 2017

Method three called by Thread : pool-1-thread-1 at Tue Apr 04 16:32:27 IST 2017


回答1:


Explanation

Spring creates a proxy around your instance. ScheduledMethod calls internally 3 methods, which are not proxified and thus not asynchronous.

cf. the documentation:

If you invoke a method on an object reference, the method is invoked directly on that object reference, as can be seen below.

See this question Spring AOP not working, when the method is called internally within a bean for a workaround, but the best is the one proposed in the doc The best approach (the term best is used loosely here) is to refactor your code such that the self-invocation does not happen...

Note that, private method is not supported too:

Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

Workaround example

@Component
public class ServiceMethod {
    private static final Logger logger = Logger.getLogger(ServiceMethod .class);

    @Async
    public void methodOne() {
        logger.info("Method one called  by Thread : " + Thread.currentThread().getName() + "  at " + new Date());
    }

    @Async
    public void methodTwo() {
        logger.info("Method two called  by Thread : " + Thread.currentThread().getName() + "  at " + new Date());
    }

    @Async
    public void methodThree() {
        logger.info("Method three called  by Thread : " + Thread.currentThread().getName() + "  at " + new Date());
    }
}

@Component
public class TaskScheduler {
    private static final Logger logger = Logger.getLogger(TaskScheduler.class);

    @Autowired
    private ServiceMethod serviceMethod;

    @Scheduled(fixedDelay = 10000)
    public void ScheduledMethod() {
        serviceMethod.methodOne();
        serviceMethod.methodTwo();
        serviceMethod.methodThree();
    }
}



回答2:


You might not have configured Thread Pool with more Threads for your Scheduler.

From the docs

If you do not provide a pool-size attribute, the default thread pool will only have a single thread.



来源:https://stackoverflow.com/questions/43204817/spring-async-method-call-inside-scheduled-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!