Blog

February 6, 2013

Announcing Play Framework 2.1 - The High Velocity Web Framework

February 6, 2013

Play Framework 2.1 has been released! The goal of Play is to empower developers to quickly build scalable apps with Java and Scala, so we've jam packed this release with features to make that a reality.

Let's take a look at few of the new updates.

Dependency Injection for Controllers

In the past, the URL to code mappings in Play only supported calling static methods on a controller. This made it difficult to use Dependency Injection (e.g. Spring Framework and Guice) to setup an application at runtime based on configuration. Play 2.1 now supports a new routes syntax that enables calling injected controllers instead of static methods. Simply prefix the controller method in the routes files with an "@" symbol, like:

GET     /                  @controllers.Application.index()

Then in your Global class use the getControllerInstance method to return the instance:

public <A> A getControllerInstance(Class<A> clazz) {
    return ctx.getBean(clazz);
}

For a complete example that uses Spring 3, check out Guillaume Bort's play20-spring-demo project on GitHub.

Better Scalability, Non-Blocking Async, and Scala 2.10

For Scala developers the most exciting new feature in Play 2.1 is probably the upgrade to Scala 2.10. This brings with it a ton of new language features. One new feature that makes it easier to build scalable Play apps is the new Futures and Promises API, which was initially created in Akka. This enables a very concise programming model for doing non-blocking, asynchronous composition of services. In a nutshell, this enables your server to handle a ton more requests since it only uses threads when it absolutely needs them. Here is a simple example that makes two JSON requests, one to GitHub and one to Twitter, then when it has the responses it renders a web page containing data from both:

def social = Action {
  Async {
    val twitterPromise = WS.url("http://search.twitter.com/search.json").withQueryString(("q", "playframework")).get()
    val githubPromise = WS.url("https://api.github.com/legacy/repos/search/playframework").get()
    for {
      twitterResponse <- twitterPromise
      githubResponse <- githubPromise
    } yield Ok(views.html.social.render((twitterResponse.json \\ "text").map(_.as[String]), (githubResponse.json \\ "name").map(_.as[String])))
  }
}

The amazing thing about this is that under the covers none of the web requests block when they are in a waiting state! Of course, you can also get the same behavior in Java using Play's Functional APIs.

Modular Routing Makes Large Projects Easier to Manage

As projects grow it is important to break them into sub-projects. Now you can easily include a sub-project's routes into the master project using the new "->" syntax in the routes file. For instance, to include all of the routes defined in a subproject, under a "/foo" URL prefix, simply add the following to your routes file:

->    /foo      foo.Routes

This enables many developers to easily collaborate on large, multi-project code bases without stepping on each others toes. For more details, read Working with Sub-Projects in the documentation.

Built For Modern & Scalable Apps

These are just a few of the many new features in Play 2.1. Over the past year, the Play Framework has been successfully used in some very demanding scenarios, like Klout's billion API transactions per day, Gilt Group's real-time inventory updates via WebSockets, and of-course for the Typesafe website. This new release makes it easier to build modern and scalable apps while being developer friendly. Head on over to the all-new playframework.com to download Play 2.1 and build something amazing!

By James Roper - Play Framework Developer at Typesafe.

comments powered by Disqus
Browse Recent Blog Posts