基于Akka的演员消息感觉就像是从一些“上游”演员到一个或多个“下游”演员的单向流/消息流。
但是,对于两个actor之间的某种双向(请求/响应)同步消息传递,可能存在合法的用例。例如,我可能有CacheActor
来管理一些缓存。其他参与者可能希望put
和get
缓存此缓存的条目,他们需要通过CacheActor
执行此操作:
// Groovy pseudo-code
class CacheActor extends UntypedActor {
Map<String,Object> cache
// ...
@Override
void onReceive(Object message) {
if(message instanceof GetCacheEntry) {
GetCacheEntry getCacheEntry = message as GetCacheEntry
String entry = cache.get(getCacheEntry.key)
// Now what? How to send back to the actor that called this?!?
getCacheEntry.sender.tell(new GetCacheEntryResult(getCacheEntry.buzz, entry)) // <-- 2. return to sender (again with 'buzz' as sidecar)
}
}
}
class FizzActor extends UntypedActor {
ActorRef cacheActor
@Override
void onReceive(Object message) {
if(message instanceof Buzz) {
Buzz buzz = message as Buzz
cacheActor.tell(new GetCacheEntry(buzz, this)) // <-- 1. identify 'this' as the sender and pass 'buzz' in as sidecar
} else if(message instanceof GetCacheEntryResult) { // <-- 3. recognize that we are processing the result from #1 above
GetCacheEntryResult result = message as GetCacheEntryResult
Buzz originalBuzz = result.buzz
String resultantEntry = result.entry
// 4. Now keep processing...
}
}
}
这是我能够设想以“会话式”请求 - 响应方式在两个演员之间来回传递消息的唯一方法。问题在于它有点讨厌:
GetCacheEntryResult
),这将快速膨胀代码;和buzz
)。在几个演员之间的一个更复杂的编排中,这个“上下文映射”会变得相当大;和所以我问:在Akka有更好的方法吗?也许使用Futures
或ask(...)
方法?