Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

My Top 5 Play Framework Features

For a year I’ve been working on two projects using the Play! Framework. The JRebel License Server and LiveRebel. I was evaluating different frameworks for these tasks and it boiled down to a choice between Struts and Play! Framework. Play! seemed the risky, cool, rebellious choice, while Struts was something more like the work horse (somewhat old) who delivers for sure. After some debate within the team we took a chance and went with Play!. During this time I’ve grown to love some features of Play! more than others and would like to share the love.

My Top 5 Play! Framework Features

Jobs

Play! framework jobs provide a way of running program logic “in the background”. Play! will take care of the lifecycle and the timings. For example if you need something to run every 5 minutes you will use an annotation @Every(“5min”) and the job will be run every 5 minutes. I’ve included an example called MemoryUsageLogger which logs the memory usage of the application. Its just dead simple.

@OnApplicationStart
@Every("5min")
public class MemoryUsageLogger extends Job {
  private volatile boolean maxPrinted = false;
 
  @Override
  public void doJob() throws Exception {
    Runtime r = Runtime.getRuntime();
 
    long total = r.totalMemory();
    long free = r.freeMemory();
    long used = total - free;
 
    if (!maxPrinted) {
      Logger.debug("Used: %dM Free: %dM Total: %dM Max: %dM", m(used), m(free), m(total), m(r.maxMemory()));
      maxPrinted = true;
    }
    else {
      Logger.debug("Used: %dM Free: %dM Total: %dM", m(used), m(free), m(total));
    }
  }
 
  private static long m(long bytes) {
    return bytes / 1024 / 1024;
  }
}

Templating

So far I’ve used JSPs (old and XML syntax), Jelly (Hudson plugins), XTemplate and Smarty for PHP as templating engines. I’ve never felt productive in these, writing custom tags that choke when changing the container, fighting with bloated XML or even just learning an obscure templating expression language has not been fun.

Play! template engine uses Groovy as an expression language and also a tag system for reusable components. Groovy is a clean, intuitive, powerful language which is really easy for Java devs to pick up. I also like that it is a separate, well established project vs something that is only meant for templating.

#{list items:resources,as:'res'}
  #{if res.directory}
    <tr class="directory collapsed" rel="${res.id}" parent="${res.parent?.id}">
      <td><a href="#">${res.name}</a></td>
      <td></td>
      <td class="right">${res.lastModified == null ? '(unknown)' : res.lastModified.format("yyyy-MM-dd HH:mm")}</td>
    </tr>
  #{/if}
#{/list}

This all comes with great error messages, even for the templating code. You can quickly tell where you’ve made a mistake. I just hope the templating language will be supported by Eclipse to provide content assist and highlighting.

URL mappings and redirects

Your controllers in Play! contain ton of static methods that adhere to the URLs of your application. For example, if you need to show something for a URL /contacts/list you would implement a method list in a controller contacts (this is the default naming convention that can be changed). Or if you want a page to show an actual contact you would have a method showContact and it will get mapped to /contacts/showcontact.

What if I want to redirect from the list page to a show contact page when there is only a single contact to show? In your Java code in the list method just invoke the showContact method, it’s going to be an HTTP redirect. So easy and readable in Java that it is scary.

Testing

Automated app testing is a difficult subject, there are tons of approaches out there and its all about you picking a couple and trying to use them with your app written in framework X. Play! has a different approach and it comes bundled with a slick interface to run unit, functional and selenium tests against your app. As testing is something that is so easy to “forget about” then something halfway enforced is really welcomed. Besides automating this you can use the same interface while developing vs configuring the tests to run from your IDE or ant/maven/shell.

Quick Turnaround

We at ZeroTurnaround are really into developer productivity. Our flagship product JRebel lets you change your application code and just hit refresh in your browser and you’ll see the changes instantly. Play! framework offers something similar. I have not read enough source code but it seems that with custom extra metadata (see your app’s tmp/bytecode/DEV), a stateless model and the custom runner (play run) they will give you the power of changing code on the fly. If they don’t support the change they will restart your app automatically for you. This is actually a really big thing compared to other frameworks out there.

Conclusions

Play! framework is packed with cool features and this is only my top 5. Of course Play! is no silver bullet and it does have its own set of problems, but that is something for another article. So far Play! has proven to be a productive and intuitive environment to develop in. What are your favourite features of Play!?


Toomas RömerToomas Römer is the co founder and product lead of ZeroTurnaround. Once a Linux junkie, he was fooled by Apple into proprietory OS and devices. He is a big fan of JUGs, OSS communities and beer. He blogs at dow.ngra.de, tweets from @toomasr and also runs the non-profit chesspastebin.com website. In his spare time he crashes Lexuses while test driving them, plays chess, Go and Starcraft. Looks can fool you; he will probably beat you in Squash. You can connect with Toomas on LinkedIn.


  • Rodrigo Reyes

    I am considering using Play for a personal project development. I would love to hear about the issues you have found with it.

  • http://twitter.com/toomasr Toomas Römer

    The main issues we have ran into are http://play.lighthouseapp.com/users/120617 and http://play.lighthouseapp.com/users/125294 Besides these I guess the biggest feature we are missing is proper maven integration.

  • http://profiles.google.com/richiethom Richard Thomas

    Re “URL mappings and redirects”, the URL /contacts/list maps automatically to the static method Contacts.list – the Contacts class doesn’t need to start with a lower case C. I know you know that, but for a new Player, that might put them off!

  • v6ak

    If fact, there is a maven intergation, but it is in another way you might want. It is a plugin for Play! build system that allows you to add Maven dependencies.

  • http://twitter.com/toomasr Toomas Römer

    I stumbled upon that too and was confused but I tested and it worked and I was happy. I guess the doc could be a bit better for that part.

  • http://twitter.com/toomasr Toomas Römer

    I don’t know if it was the correct one but when I tried it just did not work. I left a comment on the page but it never got an answer. I think I should give it another try. Currently I have an ANT task that copies some deps (managed by maven) to lib folder every now and then.

  • v6ak

    I’ve tried it and at least the ‘play mvn:re’ works correctly for me.

  • abp

    play 1.2 will have built-in dependency management via simplified maven/ivy-config in a yaml-format. ;)

  • http://twitter.com/reinra Rein

    Thats freaking awesome, do you happen to know when will this be released? Could not find on the website…

  • abp

    You can find the first alpha release under “Upcoming”:

    http://www.playframework.org/download

    Also, on the github master branch you can have a look at the current documentation without downloading the alpha:

    https://github.com/playframework/play/blob/master/documentation/manual/dependency.textile

  • http://profiles.google.com/opensas opensas opensas

    here comes my top five

    testability integrated
    works out of the box
    not a single xml file
    edit and reload
    java all the way

    and bonus: scala friendly (with a compile-checked template system!!!)

  • Jitesh

    Hi Toomas! I’m Jitesh and I work for Packt.

    We recently released a book on Play! titled: Play Framework Cookbook written by Alexander Reelsen. If you are interested in reviewing the book, then I’d be glad to send you an e-copy of it. You can get in touch with me at jitesh at packtpub dot com.

    More information about the book: Play Framework Cookbook Book & eBook | Packt Publishing Technical & IT Book and eBook Store

    Cheers!

  • http://www.zeroturnaround.com/ Toomas Römer

    Cool, sent you an email.

  • http://twitter.com/PierreDuplouy Pierre Duplouy

    Great article!
    So you’re saying Play! features something similar to JRebel regarding code changes.
    Are they compatible? Is there any added value to use them both?

  • http://www.zeroturnaround.com/ Toomas Römer

    Kind of similar but in Play! you can change Play! managed stuff so when you add JRebel it is good for library changes.