英文:
Calling method with @Async annotation called after to much time in spring boot
问题
我正在使用java-11和spring 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:
- 在大多数情况下,它会在时间内执行,但有时调用该方法可能需要多个小时。
- 线程问题。如果可用线程正在执行其他任务,您可能需要等待很多小时。
- 从同一类中调用异步方法 - 不起作用。您可以查看详细信息https://www.baeldung.com/spring-async。
- 您应该创建一个线程池,其大小应大于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.
-
calling the async method from within the same class — won't work. you may see the detail https://www.baeldung.com/spring-async
-
you should create a ThreadPool and its size should bigger than 1, you may see the detail https://spring.io/guides/gs/async-method/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论