使用EJB异步方法的正确方法 [英] Correct way to use EJB Asynchronous methods
问题描述
我需要执行两个任务,比如说 task1
和 task2
,它们是同一业务流程的一部分.当 task1
完成时,我必须给最终用户响应,因此必须将响应时间减至最少.
I have two tasks that I need to perform say task1
and task2
which are a part of same business process. I have to give response to the end user when task1
completes so it's response time have to be minimized.
我当前的方法是执行 task1
,并在 task1
完成后立即异步调用 task2
方法. task2
很复杂,并且它的响应时间超出了我的控制范围,因为它具有某些外部依赖性.
My current approach is to perform task1
and as soon as task1
is finished, invoke task2
method asynchronously. task2
is complicated and it's response time is out of my control as it has some external dependency.
@Stateless
public class SessionBean1 {
@Inject
SessionBean2 sessionBean2;
public void doTask1(){
// task one stuff
sessionBean2.doTask2();
}
}
@Stateless
public class SessionBean2 {
@Asynchronous
public void doTask2(){
// do task2 stuff
}
}
在Websphere 8.0(正在使用的EJB容器)中,同步方法和异步方法由不同的线程池运行.
In websphere 8.0 (the EJB container in use) synchronous methods and asynchronous methods are run by different thread pools.
我最初的假设是,即使 task2
执行不佳, task1
也不会产生任何影响,但可悲的是,事实并非如此.
My initial assumption was that even if task2
is performing badly, task1
would have no impact, but sadly that's not true.
如果 task2
的性能不佳,则将占用异步线程池中的所有线程.这将导致 task1
等待异步线程释放,因此 task1
具有影响.
If task2
is performing badly, all threads from asynchronous thread pool will be occupied. This will cause task1
to wait for asynchronous threads to be free and hence task1
has impact.
websphrere服务器日志中的消息:线程池WorkManager.WebSphere_EJB_Container_AsynchMethods_Internal_WorkManager的请求缓冲区已达到其容量
The message in websphrere server logs :
The request buffer for thread pool WorkManager.WebSphere_EJB_Container_AsynchMethods_Internal_WorkManager has reached its capacity
我的问题是,要实现我在这里想要实现的目标的正确方法是什么.
My question is what would be a proper way to achieve what I'm trying to achieve here.
推荐答案
另一种替代方法是在管理控制台中增加"EJB异步方法调用设置"的工作请求队列大小".这是一个队列,在实际的线程池本身之前,所以这可能会给您带来更多的时间.
Another alternative would be to increase the "Work request queue size" of the "EJB asynchronous method invocation settings" in the Admin Console. This is a queue, before the actual thread pool itself, so this might buy you some more time.
理想情况下,应与上面建议的超时结合使用.
Ideally this should be used in combination with the timeouts suggested above.
这篇关于使用EJB异步方法的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!