Skip to content

Instantly share code, notes, and snippets.

@rkuhn
Created March 10, 2012 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rkuhn/2012571 to your computer and use it in GitHub Desktop.
Save rkuhn/2012571 to your computer and use it in GitHub Desktop.
TypedActor Demonstration
package typedactordemo
import akka.actor._
import akka.dispatch.Future
import akka.pattern.ask
import akka.util.Timeout
import akka.util.duration._
case class Request(payload: String)
case class Response(payload: String)
trait Service {
def request(r: Request): Future[Response]
}
class ServiceImpl extends Service {
val actor = {
val ctx = TypedActor.context
ctx.actorOf(Props[ServiceActor])
}
implicit val timeout = Timeout(10 seconds)
def request(req: Request): Future[Response] =
(actor ? req).mapTo[Response]
}
class ServiceActor extends Actor {
def receive = {
case Request(payload) =>
sender ! Response(payload)
}
}
object Main extends App {
val system = ActorSystem("TypedActorDemo")
val service: Service =
TypedActor(system).typedActorOf(
TypedProps[ServiceImpl]()
)
val req = Request("hello world!")
service.request(req) onSuccess {
case Response(response) =>
println(response)
system.shutdown()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment