2. Play Framework
‣ Play 1 released 2008 only with Java support
‣ Play 2 released March 2012
‣ Totally rebuilt with Scala
‣ Inspired by Rails
‣ Now part of the Typesafe Framework
including Scala and Akka
Monday, 4 February 13
3. Scala
‣ Created by Martin Odersky in 2004
‣ Built on top of the JVM
‣ Both functional and OO
‣ Influenced by Haskell, Erlang, Smalltalk
Monday, 4 February 13
4. Play 2 core features
‣ Hot reload
‣ Testing frameworks: JUnit, Selenium
‣ DB: Anorm, Ebean
‣ Groovy: templates as simple functions
‣ Sbt for dependency management
‣ Akka
Monday, 4 February 13
5. Akka
‣ Actor library for Java and Scala
‣ Inspired by the Erlang actors model
‣ Defines also Future and Promises now part
of the Scala language as of v2.10 released
Jan 4 2013
Monday, 4 February 13
6. Actors
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
class MyActor extends Actor {
val log = Logging(context.system, this)
def receive = {
case "test" => log.info("received test")
case _ => log.info("received unknown
message")
}
}
object Main extends App {
val system = ActorSystem("MySystem")
val myActor = system.actorOf(Props[MyActor],
name = "myactor")
myActor ! "test"
}
Monday, 4 February 13
7. Futures
With actors
import scala.concurrent.Future
import akka.pattern.ask
val future: Future[String] = ask(actor, msg).mapTo[String]
Directly
val f: Future[List[String]] = future {
session.getRecentPosts
}
f onComplete {
case Success(posts) => for (post <- posts) println(post)
case Failure(t) => println("An error has occured: " + t.getMessage)
}
Monday, 4 February 13
8. Promises
import scala.concurrent.{ future, promise }
import scala.concurrent.ExecutionContext.Implicits.global
val p = promise[T]
val f = p.future
val producer = future {
val r = produceSomething()
p success r
continueDoingSomethingUnrelated()
}
val consumer = future {
startDoingSomething()
f onSuccess {
case r => doSomethingWithResult()
}
}
Monday, 4 February 13
9. Usage in Play 2.0.4
def index = Action {
val promiseOfInt: Promise[Int] = Akka.future {
intensiveComputation()
}
Async {
promiseOfInt.map(i => Ok("Got result: " + i))
}
}
Play 2.1 will use Scala promises
Monday, 4 February 13
10. JBoss Netty
‣ Framework for writing async network
applications.
‣ Support for:
‣ SPDY
‣ WebSockets
‣ Different from Jetty (a servlet container)
Monday, 4 February 13