Can i suggest an alternative? Why not use actors to model this computation?
For example, you could have a class ProcessAssetsActor. The class has a
Map<Long,Future> called assetFutures, and a List<Asset> called assets
containing the received assets. In the preStart() method, you create a
future to get the asset ids and pipeTo(self). in your receive loop, you
wait for the initial list of asset ids. once that is received, you
create a future (again using pipeTo(self)) to get the asset for each
assetId, and put each assetId and future in assetFutures. as each Asset
is received in the receive loop, remove the reference from assetFutures
and add the Asset to assets. when assetFutures is empty, you are done
computing. passing the result back to the client can be done a few ways;
you could for example have the ProcessAssetsActor send the result to the
parent automatically, or you could have the parent actor send a query
message to the ProcessAssetsActor (using ask() or tell()) and wait for a
response message.
This probably seems like a lot of extra work, but i like this pattern
for a couple reasons:
1) by using actors you have access to much better error handling.
2) as your business requirements change, it is easier to modify your
business logic in an actor than in a chain of futures.
-Michael
On 10/07/14 13:51, Joe Wong wrote:
Hi,
I have the following events that I'm trying to put into a non blocking
way. The events are as follows
1. database call to get list of asset ids.
2. create a list of futures to get assets (getting assets can take a
while and are independent of one another)
3. process all the assets and return a Result.
What I have is the following
|
public Future<Result> buildFuture(){
//Blocking DB call
Collection<Long> assetIds = assetDao.getAssetIds();
Collection<Future<Asset>> assetFutures = new ArrayList<>(assetIds.size());
for (Long assetId : assetIds) {
assetFutures.add(createFutureAsset(assetId, executionContext));
}
Future<Collection<Asset>> futureAssets =
sequence((Iterable)assetFutures, executionContext);
//once we have all Assets process them and return result
Future<Result> futureResult = futureAssets.map(
new Mapper<Collection<Asset>, Result>() {
@Override
public Result apply(Collection<Asset> assets) {
//do something with the assets
return new Result("done");
}
},
executionContext
);
return futureResult;
}
|
What I'm trying to get my head around is how to wrap all 3 events into
one Future<Result>. Any ideas? Thanks in advance
Regards
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google
Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
--
Read the docs: http://akka.io/docs/
Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.