Does the request template work with Acre IO actors?

Say I have an IO Actor connectioncapable of sending and receiving messages over TCP. In my actor, I ask the other side of the connection for an answer:

 connection.ask(ByteString("stuff")).collect {
    case Received(response) => log.debug(response.utf8String)
 }

It looks like with this code, set the timeout, and instead the contained participant receives an unhandled message outside the request template.

Can you use a query template with Akka IO actors? If not, why not?

+4
source share
2 answers

I do not know the architecture in detail, but here is how I will explain it to myself:

Akka IO , -. - , TCP -. TCP . TCP- - - . .

Akka IO - , , . TCP , : Received , Connect. . , , , - , .

, ask , A - , B, A, , TCP- - Connect.

, , , ask "phantom", , ask. "phantom" , Future. - , , .. !

, , , , ask, , Akka IO, . , Akka IO, . , TCP- -.

+2

@ghik, , , - .

class IOAskHandlerActor(address: InetSocketAddress) extends Actor {
   override def receive = {
      // Connection setup code omitted    
      case Connected(remote, local) =>
         // other code omitted
         context become latch(sender())
   }

   def latch(connection: ActorRef): Receive = {
      case outgoing =>
         context become receiving(connection, sender())
         connection ! MySerializer.write(outgoing)
   }

   def receiving(connection: ActorRef, asker: ActorRef): Receive = {
      case Received(incoming) =>
         context become latch(connection)
         asker ! MySerializer.read(incoming)
   }
}

ask ed . , ( ), , , .

0

Source: https://habr.com/ru/post/1539946/


All Articles