Month: March 2013

Migrating to Play 2.1

Play 2.1 Migration

I have just finished migrating one of my apps from Play 2.0.4 to 2.1 and while doing that I compiled a list of all the big & little things that I had to adapt. My app uses many parts of Play (except Anorm) so hopefully it will cover a lot of problems you will encounter. My app is written entirely in Scala, so changes in the Java API will (mostly) not be covered. First of all you should read the official migration guide.

Compilation

Compilation times seem to have become even slower than before. Even on simple project with only a few files you can wait a good amount of time after changing a single file. I could actually trace this back to the fact that even when running play ~run (automatic compilation on file change, only useful when you’re not using an IDE) doesn’t prevent a re-compile when refreshing the browser.

What’s gone

Mixing Scala and Java API and using templates doesn’t seem to be possible anymore. As soon as you add javaCore to your dependencies, your templates will expect their types from either the Scala or Java API, whereas before it would choose the correct one for you.

Using Option in path parameters (they removed the PathBindable). You can implement your own PathBindable for this (as shown here), however they are right in that it doesn’t make much sense to have an optional value in your path.

What’s changed

Scala Futures

Play 2.0’s Promise has been replaced by Scala 2.10’s Future. This also means that instead of Akka.future {} you now wrap code that should be computed asynchronously with scala.concurrent.Future {}. And don’t forget to bring the right execution context in scope. The default execution context in Scala is scala.concurrent.ExecutionContext.Implicits.global, Play offers another one with play.api.libs.concurrent.Execution.Implicits._). If you have some blocking code, make sure to use a different context so the server thread won’t block.

More infos in the official documentation and in this excellent introduction.

JSON

The JSON parser has been rewritten. For one, the reads method no longer returns the value directly, but is wrapped in a JsResult. JsResult is a monadic structure which can be constructed using validate and allows for our well-known for comprehension:

implicit object UserReads extends Reads[User] {
  def reads(json: JsValue): JsResult[User] = for {
    name < - (json  "name").validate[String]
    email <- (json  "email").validate[String]
    password <- (json  "password").validate[String]
  } yield User(name, email, password)
}

▸ Read more...

Posted in Scala & Playframwork