EJB
我有一个调用 4秒的“客户端” ,Asynchronous
EJB
应该让它们全部运行 5 秒。Future
5 秒后,“客户端”EJB 从完成运行的对象收集就绪结果,并返回输出。
我对客户端部分的“等待”有疑问。我试图调用future.get(5, TimeUnit.MILLISECONDS)
它似乎有时 async EJB
s在超时后开始运行。有正确的方法吗?
1)收集Future
对象Map
:
Map<String, Future> futureMap = new HashMap<String, Future>();
for (String userID: users) {
Future<Set<FrontPageData>> test = util.asyncGetData(userID);
futureMap.put(serviceID, test);
}
return futureMap;
Future
2)然后我从对象中获得超时输出
final long now = Calendar.getInstance().getTimeInMillis();
final long end = now + TimeUnit.SECONDS.toMillis(5)
Map<String, Object> output = new HashMap<String, Object>();
Object data;
for (String userID: futureMap.keySet()) {
Future future= futureMap.get(userID);
try {
//check how much time left till the end
long timeout = end - Calendar.getInstance().getTimeInMillis();
data = future.get(timeout, TimeUnit.MILLISECONDS);
output.put(userID, data);
} catch (Exception e) {//write to logs
}
}
return output;
}
谢谢你