Created
April 25, 2013 14:12
-
-
Save vikraman/5460007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smoke._ | |
import akka.actor._ | |
import akka.routing.RoundRobinRouter | |
import akka.pattern.ask | |
import akka.util.Timeout | |
import scala.concurrent.duration._ | |
import scala.concurrent.Await | |
object NotFoundException extends Exception("Not found") | |
class HelloResponder extends Actor { | |
def receive = { | |
case GET(Path(id)) ⇒ | |
println("hello actor called") | |
sender ! Response(Ok, body = "Hello " + id) | |
} | |
} | |
class ByeResponder extends Actor { | |
def receive = { | |
case GET(Path(id)) => | |
println("bye actor called") | |
sender ! Response(Ok, body = "Bye " + id) | |
} | |
} | |
class RootResponder extends Actor { | |
val system = ActorSystem("smoke") | |
val helloPool = system.actorOf(Props[HelloResponder].withRouter(RoundRobinRouter(8))) | |
val byePool = system.actorOf(Props[ByeResponder].withRouter(RoundRobinRouter(8))) | |
implicit val timeout = Timeout(1 seconds) | |
val duration = 1 second | |
def receive = { | |
case req: Request if req.path.startsWith("/hello") => | |
//val req0 = req.clone(path=req.path.substring(6)) | |
print(req) | |
sender ! Await.result(helloPool ? req mapTo(manifest[Response]), duration) | |
case req: Request if req.path.startsWith("/bye") => | |
sender ! Await.result(byePool ? req mapTo(manifest[Response]), duration) | |
case _ ⇒ sender ! Status.Failure(NotFoundException) | |
} | |
} | |
object ActorPoolExampleApp extends Smoke { | |
val rootPool = system.actorOf(Props[RootResponder].withRouter(RoundRobinRouter(8))) | |
onRequest(rootPool ? _ mapTo manifest[Response]) | |
onError { | |
case NotFoundException ⇒ Response(NotFound) | |
case e: Exception ⇒ Response(InternalServerError, body = e.getMessage) | |
} | |
after { response ⇒ | |
val headers = response.headers + ("Server" -> "ActorPoolExampleApp/0.0.1") | |
Response(response.status, headers, response.body) | |
} | |
beforeShutdown { | |
println("Shutdown...") | |
} | |
afterShutdown { | |
println("Shutdown complete!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment