Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda