About Play already wrote on Habré:
Excellent Java MVC framework and the
'Hello World' tutorial . In this article, Thomas Römer from
ZeroTurnaround talks about his favorite tweaks in Play! framework.
Over the past year I have been developing two projects with Play! Framework. This is the
JRebel and
LiveRebel license server . I tried different frameworks for these two tasks, and in the end my choice came down to two candidates:
Struts and
Play! Framework . Play! seemed more
risky, fun, rebellious , while Struts was considered something of an old workhorse that works and is guaranteed to achieve results. After some disputes in the team, we decided to take a chance and try Play! .. Since then I have loved some of the features of Play! more than others and would like to share my love.
')
My 5 favorite fishes in Play! Framework
- Tasks (Jobs) - perform work in the background
- Patterns - intuitive and productive
- Mapping and URL redirection - horror as easy
- Testing - forced and half done for you
- Quick changes - happy and productive developers
- findings
Tasks (Jobs)
Tasks in Play! frameworks offer a way to run program logic in "background mode". Play! take care of their life cycle and time of execution. For example, if you want to run something every five minutes, you simply use the
@Every(“5min”)
annotation
@Every(“5min”)
, and the task will be executed every 5 minutes. I will give for example the class
MemoryUsageLogger
, which logs the amount of memory used. Kill yourself how easy it is!
@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; } }
Templates
So far I have used JSP (old XML-like syntax), Jelly (in Hudson plugins), XTemplate and Smarty for templates in PHP. I have never felt particularly productive with them. Writing customized tags that fly to hell as soon as you change the container, struggle with bloated XML files, or even just learning the muddy expression language for templates — you can't call it fun.
Play templates! use Groovy as an expression language, as well as its tag system for reusable components. Groovy is a clean, intuitive, powerful and truly simple language, especially easy for Java developers. I also like the fact that Groovy is something independent, a separate promoted project, and not something special, created only for templates.
#{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}
By the way, all this is provided with great error messages, even for templates. You can quickly understand where you made a mistake. I hope that the template language will be supported by Eclipse to provide autofill and syntax highlighting.

URL mappings and redirects (URL mappings and redirects)
Your controllers in Play! contain a bunch of static methods that are bound to your application's URLs. For example, if you need to show something via the
/contacts/list
link, you need to write the
list
method in the
contacts
controller (this is the default naming convention, which can be redefined if necessary). Or if you need a page that shows contact information, you create a
showContact
method and it will be linked to the
/contacts/showcontact
.
What if you want to redirect the user from the page with a list of contacts to the
/contacts/showcontact
page if there is only one contact in the list? In your Java code in the
list
method, just call the
showContact
method, and Play! will check this as an external redirect (http redirect). So easy and readable that it becomes scary.
Testing
Automated application testing is a hard question. There are many approaches, and usually you have no choice but to choose a couple of them and try on your application written in the X. Play framework! uses a different approach: it comes bundled with a nice interface for running unit, functional, and selenium tests on your application. Since testing is such a thing that “easy to forget” is about, it is always convenient when it starts “somehow by itself”. In addition to the automation of testing, you can use the same interface during the development process (usually for this you have to configure the launch of tests from IDE or ant / maven / shell scripts).

Quick turnaround
We at
ZeroTurnaround (literally "zero turnover" or "instantaneous response") are obsessed with developer productivity. Our flagship product
JRebel allows
you to change the application code and simply click the "refresh" button in the browser to instantly see your changes on the screen. Play Framework! offers similar functionality. I have not yet had enough time to study the source code of Play !, in detail, but it seems that due to several small restrictions (some meta-information in bytecode, see tmp / bytecode / DEV, a stateless model, and a customized
play run
) it Offers you the powerful ability to change code on the fly. If at some point changing the code "on the fly" is not possible, it will automatically restart your application quietly. This is a huge advantage over other frameworks.
findings
Play Framework! suggests cool things, and these are just 5 of my favorites. Of course, play! - This is not a silver bullet, and it has its own problems, but it is a topic for a separate article. During the time I was working with Play !, it proved to be a productive and intuitive environment for developing web applications. And what are your favorite pieces of Play !?

Toomas Römer, co-founder and product lead of ZeroTurnaround, is the one that made
JRebel . Thomas is a big fan of JUGs, OSS communities and beer. He writes blogs at
dow.ngra.de , tweets like @toomasr, and also supports the non-commercial site
chesspastebin.com . In his spare time, he smashes Lexus in drive tests, plays chess, Go, and Starcraft. His appearance is deceptive; he will surely make you squash. If anything, he and
LinkedIn have an account.