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!?
-
Rodrigo Reyes
-
http://twitter.com/toomasr Toomas Römer
-
http://profiles.google.com/richiethom Richard Thomas
-
v6ak
-
http://twitter.com/toomasr Toomas Römer
-
http://twitter.com/toomasr Toomas Römer
-
v6ak
-
abp
-
http://twitter.com/reinra Rein
-
abp
-
http://profiles.google.com/opensas opensas opensas
-
Jitesh
-
http://www.zeroturnaround.com/ Toomas Römer
-
http://twitter.com/PierreDuplouy Pierre Duplouy
-
http://www.zeroturnaround.com/ Toomas Römer
