I've modified Echo server from https://github.com/akka/akka/blob/master/akka-docs/rst/scala/code/docs/io/EchoServer.scala#L96
我已经从https://github.com/akka/akka/blob/master/akka-docs/rst/scala/code/docs/io/EchoServer.scala#L96修改了Echo服务器
case Received(data) =>
connection ! Write(data, Ack(currentOffset))
log.debug("same {}", sender.eq(connection)) // true
buffer(data)
That means incoming and outgoing messages are handled by the same actor. So a single working thread(that takes messages from a mailbox) will process read and write operations.
这意味着传入和传出消息由同一个actor处理。因此,单个工作线程(从邮箱接收消息)将处理读写操作。
In "classical" world I can create one thread to read from a socket and another for a writing and get simultaneous communication.
在“经典”世界中,我可以创建一个线程来从套接字读取,另一个线程用于写入并获得同步通信。
1 个解决方案
#1
Akka remoting allows you to use pluggable transport implementations, so you can chose how TCP or any other transport protocol is implemented. By default you get enabled-transports = ["akka.remote.netty.tcp"]
.
Akka远程处理允许您使用可插拔传输实现,因此您可以选择如何实现TCP或任何其他传输协议。默认情况下,您将获得enabled-transports = [“akka.remote.netty.tcp”]。
TCP is a full-duplex protocol. Sockets support full-duplex. So nothing stops you on the transport layer.
TCP是全双工协议。套接字支持全双工。所以没有什么能阻止你在传输层上。
Final thing to verify if application layer, i.e. Akka remoting can support full-duplex. Every message Akka receives will be forwarded to destination actor. We know obviously that message handling is asynchronous, so while one actor is processing one message another actor can send or receive messages as long as your transport protocol allows it (which it does in this case).
最后要验证应用层,即Akka远程处理是否支持全双工。 Akka收到的每条消息都将被转发给目标演员。我们很明显知道消息处理是异步的,所以当一个actor处理一个消息时,只要你的传输协议允许,它就可以发送或接收消息(在这种情况下它就是这样)。
However, message processing in a single actor is synchronous: any given actor can only send or receive a message at one point in time. This means that if you are sending messages via connection
while Akka remoting is also receiving messages destined to your actor those received messages will get queued in the actor message queue. Akka remoting will be able to send and receive at the same time, but your actor will go through it's queue as normal: pop, process, [send], rinse and repeat.
但是,单个actor中的消息处理是同步的:任何给定的actor只能在一个时间点发送或接收消息。这意味着如果您通过连接发送消息,而Akka远程处理也接收发往您的actor的消息,那么这些接收的消息将在actor消息队列中排队。 Akka遥控器可以同时发送和接收,但你的演员将正常通过它的队列:弹出,处理,[发送],冲洗和重复。
Getting back to your question: you can expect Akka to do asynchronous send/receive over TCP for you. You don't work with a socket directly when using Akka Remoting. Instead, Akka (netty) runs a channel selector and asynchronously receives TCP messages and sends them to observers/callbacks - internal to Akka. Those get converted to Akka application messages and sent to your actor (added to the queue).
回到你的问题:你可以期待Akka为你做基于TCP的异步发送/接收。使用Akka Remoting时,您无法直接使用套接字。相反,Akka(netty)运行一个通道选择器并异步接收TCP消息并将它们发送到观察者/回调 - Akka内部。这些转换为Akka应用程序消息并发送给您的actor(添加到队列中)。
#1
Akka remoting allows you to use pluggable transport implementations, so you can chose how TCP or any other transport protocol is implemented. By default you get enabled-transports = ["akka.remote.netty.tcp"]
.
Akka远程处理允许您使用可插拔传输实现,因此您可以选择如何实现TCP或任何其他传输协议。默认情况下,您将获得enabled-transports = [“akka.remote.netty.tcp”]。
TCP is a full-duplex protocol. Sockets support full-duplex. So nothing stops you on the transport layer.
TCP是全双工协议。套接字支持全双工。所以没有什么能阻止你在传输层上。
Final thing to verify if application layer, i.e. Akka remoting can support full-duplex. Every message Akka receives will be forwarded to destination actor. We know obviously that message handling is asynchronous, so while one actor is processing one message another actor can send or receive messages as long as your transport protocol allows it (which it does in this case).
最后要验证应用层,即Akka远程处理是否支持全双工。 Akka收到的每条消息都将被转发给目标演员。我们很明显知道消息处理是异步的,所以当一个actor处理一个消息时,只要你的传输协议允许,它就可以发送或接收消息(在这种情况下它就是这样)。
However, message processing in a single actor is synchronous: any given actor can only send or receive a message at one point in time. This means that if you are sending messages via connection
while Akka remoting is also receiving messages destined to your actor those received messages will get queued in the actor message queue. Akka remoting will be able to send and receive at the same time, but your actor will go through it's queue as normal: pop, process, [send], rinse and repeat.
但是,单个actor中的消息处理是同步的:任何给定的actor只能在一个时间点发送或接收消息。这意味着如果您通过连接发送消息,而Akka远程处理也接收发往您的actor的消息,那么这些接收的消息将在actor消息队列中排队。 Akka遥控器可以同时发送和接收,但你的演员将正常通过它的队列:弹出,处理,[发送],冲洗和重复。
Getting back to your question: you can expect Akka to do asynchronous send/receive over TCP for you. You don't work with a socket directly when using Akka Remoting. Instead, Akka (netty) runs a channel selector and asynchronously receives TCP messages and sends them to observers/callbacks - internal to Akka. Those get converted to Akka application messages and sent to your actor (added to the queue).
回到你的问题:你可以期待Akka为你做基于TCP的异步发送/接收。使用Akka Remoting时,您无法直接使用套接字。相反,Akka(netty)运行一个通道选择器并异步接收TCP消息并将它们发送到观察者/回调 - Akka内部。这些转换为Akka应用程序消息并发送给您的actor(添加到队列中)。