不推荐在应用服务器中管理你的线程。如果您使用 EJB,规范不允许.
为什么不使用缓存解决方案来提高性能?最初的几个请求会比较慢,但是一旦缓存很热,一切都会非常快。
如果缓存数据不可行,那么改变客户端向服务器发出多个请求,而不是将一个请求拆分为多个线程呢?您需要更改您的 Web 应用程序,以便每个方法都调用一个 Web 服务。客户端将调用(并行)当前页面所需的每个方法并组装最终结果(如果您愿意,可以显示部分结果)。通过这样做,您将并行工作并且不会违反规范。
我假设您的服务器中有这样的东西:
public Result retriveData(Long id) {
Result myResult = new Result();
//...
//do some stuff
myResult.setSomeData(slowWebService1.retriveSomeData(id));
myResult.setSomeOtherData(slowWebService2.retriveSomeOtherData(id));
myResult.setData(slowWebService3.retriveData(id));
return myResult;
}
在您的客户中:
Result result = webApplication.retriveData(10);
//use the result
我的建议是将调用拆分为多种方法:
public SomeData retriveSomeData(Long id) {
//do some stuff
SomeData data = slowWebService1.retriveSomeData(id);
//do more stuff
return data;
}
public SomeOtherData retriveSomeOtherData(Long id) {
//do some stuff
SomeOtherData data = slowWebService2.retriveSomeOtherData(id);
//do more stuff
return data;
}
public Data retriveData(Long id) {
//do some stuff
Data data = slowWebService3.retriveData(id);
//do more stuff
return data;
}
在您的客户中:
//Call these methods in parallel, if you were using Swing, this could be done with
//SwingWorker (I have no idea how to it with Flash :)).
//You can either wait for all methods to return or show partial results.
callInBackground(webApplication.retriveSomeData(10), useDataWhenDone);
callInBackground(webApplication.retriveSomeOtherData(10), useDataWhenDone);
callInBackground(webApplication.retriveData(10), useDataWhenDone);
通过这样做,您只调用您的 Web 应用程序,就像以前一样,因此不应该有任何安全问题。
我不熟悉 Websphere,所以我不知道使用它的异步 bean 是否比这更好,但恕我直言,您应该避免手动启动线程。