我得到了关于 play 2 是如何构建的混合信息。它在网站上说它是建立在 akka 上的,这很可能意味着它每个连接使用一个 Actor ?它还说不要编写阻塞代码,因为它可以防止其他事件在不同的连接中触发(有点像它如何阻塞 node.js 中的事件循环)。这怎么会是因为阻止 1 个参与者中的代码不会阻止另一个参与者中的代码?这不是使用actor 与像node.js 这样的回调的重点吗?
最佳答案
当他们说 Play 建立在 Akka 之上时,他们指的是框架而不是网络服务器。连接由作为嵌入式 Web 服务器的 JBoss Netty ( http://netty.io/ ) 处理。
连接可以绑定(bind)(或不绑定(bind))到 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() 发送消息并且不等待响应。
关于java - play framework 2 中的每个连接是如何处理的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15212487/