You can avoid sending the future as a message using a template, in Worker1 you should write:
pipe(w2 ? "do work") to sender
Instead:
sender ! (w2 ? "do work")
Now r will be Future[String] instead of Future[Future[String]] .
Update: The above pipe solution is a general way to avoid your actorโs response to the future. As Victor points out in the comment below, in this case, you can completely exit the Worker1 cycle by telling Worker2 to respond directly to the actor so that he ( Worker1 ) receives a message from:
w2.tell("do work", sender)
It will not be an option if Worker1 responds in some way from a response from Worker2 (using map on w2 ? "do work" , combining several futures with flatMap or for understanding, etc.), but if this is not necessary, this The version is cleaner and more efficient.
This kills one Await.result . You can get rid of another by writing something like the following:
val response: Future[HttpResponse] = r.mapTo[String].map { c => val resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK) resp.setContent(ChannelBuffers.copiedBuffer(c, CharsetUtil.UTF_8)) resp }
Now you just need to enable this Future on TwitterFuture . I cannot tell you how to do this, but it should be pretty trivial , and definitely does not require blocking.
Travis brown
source share