我收到有关如何构建游戏2的混合信息。它在网站上说它建立在akka上,这很可能意味着它每个连接都使用一个actor?它还说不要编写阻塞代码,因为它阻止其他事件在不同的连接中触发(有点像它阻止node.js中的事件循环)。怎么可能因为1个actor中的阻塞代码没有阻止另一个actor中的代码?是不是像使用node.js那样使用actor vs回调?
答案 0 :(得分:2)
当他们说Play建立在Akka之上时,他们意味着框架而不是网络服务器。连接由嵌入式Web服务器JBoss Netty(http://netty.io/)处理。
连接可以绑定(或不绑定)给Akka actor以提供异步响应,如下所述:http://www.playframework.com/documentation/2.1.0/JavaAkka
actor之间的通信是非阻塞的,因为它们发送消息(任何对象)并且不等待响应(它们不调用方法与不同的actor通信)。
逻辑类似于:
//somewhere in the code
ActorB.tell(new Request(...));
-----------------------------------------
ActorB:
public void onReceive(Object msg) throws Exception {
if (msg instanceof Request) {
//process the request
ActorA.tell(new Response(...))
}
}
----------------------------------------------------------------
ActorA:
//ActorA is notified
public void onReceive(Object msg) throws Exception {
if (msg instanceof Response) {
System.out.println("Response: " + msg)
}
}
方法tell()发送消息,不等待响应。