I have a "client" EJB
that invokes 4 Asynchronous
EJB
s and should give them all 5 seconds to run. After 5 seconds the "client" EJB collects the ready results from Future
object that finished running, and returns output.
I have a problem with the "waiting" in client part. I tried to invoke future.get(5, TimeUnit.MILLISECONDS)
It seems like sometimes async EJB
s start to run after the timeout.
Is there a correct way to do it?
1) Collect Future
objects in 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;
2) Then I get output with timeout from Future
objects
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;
}
thank you