Randomize Randomize - 1 month ago 12x
Scala Question

Akka: pipeto self and parent

In my actor I have this sequence of messages:

var sndr: ActorRef = _

override def receive: Receive = {
case msg @ Message =>
sndr = sender
HttpRequest.singleRequest(...) pipeTo self

case smt @ HttpResponse(StatusCodes.OK, _, entity1, _) => sndr ! SomethingElse(smt)


I am saving the original "sender" in a variable. Is this the best way to send the final message to the original sender (as with the
message it becomes the


Don't keep the sender in a variable. What if there are multiple messages from different callers before the first one is complete? Only the last one would be routed correctly. Try this:

override def receive: Receive = {
  case msg @ Message => 
      HttpRequest.singleRequest(...).map(SomethingElse(_)) pipeTo sender