synchronization - Akka and two-way actor conversations -
akka-based actor messaging feels one-way stream/flow of messages "upstream" actor 1 or more "downstream" actors.
but there might legitimate use case kind of two-way (request/response) synchronous messaging between 2 actors. instance, might have cacheactor
manages cache. other actors might want put
, get
cache entries cache, , need through cacheactor
:
// groovy pseudo-code class cacheactor extends untypedactor { map<string,object> cache // ... @override void onreceive(object message) { if(message instanceof getcacheentry) { getcacheentry getcacheentry = message getcacheentry string entry = cache.get(getcacheentry.key) // what? how send actor called this?!? getcacheentry.sender.tell(new getcacheentryresult(getcacheentry.buzz, entry)) // <-- 2. return sender (again 'buzz' sidecar) } } } class fizzactor extends untypedactor { actorref cacheactor @override void onreceive(object message) { if(message instanceof buzz) { buzz buzz = message buzz cacheactor.tell(new getcacheentry(buzz, this)) // <-- 1. identify 'this' sender , pass 'buzz' in sidecar } else if(message instanceof getcacheentryresult) { // <-- 3. recognize processing result #1 above getcacheentryresult result = message getcacheentryresult buzz originalbuzz = result.buzz string resultantentry = result.entry // 4. keep processing... } } }
this way can envision passing messages , forth between 2 actors in "conversational" request-response style. problem bit nasty:
- i have create new message types (e.g.
getcacheentryresult
) each response sent requester, bloat code; and - each time 1 actor responds another, of "context variables" "in scope" need bundled , sent requesting actor sidecars, available requester when receive answer , need continue processing (see
buzz
below). in more complicated orchestration between several actors, "context map" going big; and - at end of day, solution feels code smell me...
so ask: is there better way in akka? maybe using futures
or ask(...)
method?
Comments
Post a Comment