使用@Async注解的方法在Spring Boot中调用后经过了太长时间

huangapple go评论62阅读模式
英文:

Calling method with @Async annotation called after to much time in spring boot

问题

我正在使用java-11spring boot 2.3.8.RELEASE在我的项目中。

在我的项目中,我在调用**@async**方法时遇到了问题。

当我调用**@async**方法时,有时会在很多小时后才被调用。在大多数情况下,它会按时执行,但有时会花费很多小时才调用该方法。

以下是我的代码

public void methodA(){
     logger.info("going to call async method");
     this.methodB();
     logger.info("async method execution completed");
}

@Async
public void methodB(){
      logger.info("inside async method");
}

当我检查上述日志时,我得到以下输出

2023-05-29 03:36:36.893  INFO 3382750 --- [http-nio-auto-1-exec-236] com.demo.web.service      : going to call async method

2023-05-29 04:53:08.893  INFO 3382750 --- [http-nio-auto-1-exec-236] com.demo.web.service      : inside async method

2023-05-29 04:53:08.893  INFO 3382750 --- [http-nio-auto-1-exec-236] com.demo.web.service      : async method execution completed

我不明白为什么会发生这种情况。
有时它按预期正常工作,有时却像这样工作。

请指导。谢谢

英文:

I am using java-11 and spring boot 2.3.8.RELEASE in my project.

In my project, I got the issue in calling @async method.

when i call @async method sometimes it called after to many hours. in most of the cases it executes at the time but sometime it takes to many hours to call the method.

below is my code

public void methodA(){
     logger.info("going to call async method");
     this.methodB();
     logger.info("async method execution completed");
}

@Async
public void methodB(){
      logger.info("inside async method");
}

when i checked logs for above then it i got o/p as below

2023-05-29 03:36:36.893  INFO 3382750 --- [http-nio-auto-1-exec-236] com.demo.web.service      : going to call async method

2023-05-29 04:53:08.893  INFO 3382750 --- [http-nio-auto-1-exec-236] com.demo.web.service      : inside async method

2023-05-29 04:53:08.893  INFO 3382750 --- [http-nio-auto-1-exec-236] com.demo.web.service      : async method execution completed

I don't understand why this is happening.
Sometime it works well as expected and sometime it's working like this

Any help is appreciated

Please guide. Thanks

答案1

得分: 1

Sure, here are the translated portions:

  1. 在大多数情况下,它会在时间内执行,但有时调用该方法可能需要多个小时。
  2. 线程问题。如果可用线程正在执行其他任务,您可能需要等待很多小时。
  3. 从同一类中调用异步方法 - 不起作用。您可以查看详细信息https://www.baeldung.com/spring-async
  4. 您应该创建一个线程池,其大小应大于1,您可以查看详细信息https://spring.io/guides/gs/async-method/
英文:

> . in most of the cases it executes at the time but sometime it takes to many hours to call the method.
> Blockquote

thread issue. If the available thread is busy for other task, you must wait maybe many hours.

  1. calling the async method from within the same class — won't work. you may see the detail https://www.baeldung.com/spring-async

  2. you should create a ThreadPool and its size should bigger than 1, you may see the detail https://spring.io/guides/gs/async-method/

huangapple
  • 本文由 发表于 2023年6月1日 14:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定