Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
Spring @Async method call inside @Scheduled method

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

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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();
    }
}
about 4 years ago · Santiago Trujillo Report

0

You might have not 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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!