Using futures with actors
In this recipe, we will see how to use a future with an actor, and how to retrieve the result of a computation done by a future.
Getting ready
Just import the Hello-Akka
project, and the other prerequisites are the same as earlier.
How to do it...
The following are the steps to use a future with an actor and to retrieve a result of the computation:
- Create a scala file,
FutureWithActor.scala
, in thecom.packt.chapter4
package. - Add the following imports to the top of the file:
import akka.actor.{Actor, ActorSystem, Props} import akka.pattern.ask import akka.util.Timeout import scala.concurrent.Await import scala.concurrent.duration._
- Create an actor that calculates the sum of two integers and sends it back to the sender, as shown in the following code snippet:
class ComputationActor extends Actor { def receive = { case (a: Int, b: Int) => sender ! (a + b) } }
- Create the sample...