My 5 favorite chips in the Play Framework

Original author: Toomas Römer
  • Transfer
About Play already wrote on a habr: Excellent Java MVC framework and 'Hello World' tutorial . In this article, Thomas Römer of ZeroTurnaround talks about his favorite pieces on Play! framework.

Over the past year, I have been developing two projects using 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, funny, rebelliouswhile the Struts was considered a bit of an old workhorse that works and is guaranteed to achieve results. After some debate 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 chips on the Play! Framework


  1. Tasks (Jobs) - do the work in the background
  2. Templates - Intuitive and Productive
  3. URL Mappings and Redirects - Horror as Easy
  4. Testing - forced and half done for you
  5. Quick changes - happy and productive developers
  6. findings


Tasks (Jobs)



Tasks on the Play! The framework offers a way to run program logic in the background. Play! will take care of their life cycle and lead time. For example, if you want to run something every five minutes, you just use the annotation @Every(“5min”)and the task will run every 5 minutes. I will give for example a class MemoryUsageLoggerthat logs the amount of memory used. Kill how easy it is!

@OnApplicationStart@Every("5min")
publicclassMemoryUsageLoggerextendsJob{
  privatevolatileboolean maxPrinted = false;
  @OverridepublicvoiddoJob()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));
    }
  }
  privatestaticlongm(long bytes){
    return bytes / 1024 / 1024;
  }  
}


Patterns


So far I have used JSP (old XML-like syntax), Jelly (in Hudson plugins), XTemplate and Smarty for templates in PHP. I never felt particularly productive with them. Writing customized tags that fly into tartaras as soon as you change the container, fighting inflated XML files, or even just learning the muddy language of expression for templates - all this can not be called fun.

Play Templates! use Groovy as an expression language, as well as their tag system for reusable components. Groovy is a clean, intuitive, powerful, and truly simple language, especially easy for Java developers. I also like that Groovy is something independent, a separate untwisted project, and not something special, created only for templates.

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


By the way, all this comes 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 autocomplete and syntax highlighting.


URL mappings and redirects


Your controllers on Play! contain a bunch of static methods that are bound to the URLs of your application. For example, if you need to show something by reference /contacts/list, you need to write a method listin the controller contacts(this is the default naming convention, which can be overridden if necessary). Or if you need a page showing contact information, you create a method showContactand it will be attached to the link /contacts/showcontact.

What if you want to redirect the user from the page with the list of contacts to the page /contacts/showcontactif there is only one contact in the list? In your Java code in a method, listjust call the methodshowContactand Play! will do this as an external redirect (http redirect). So easy and readable that it gets scary.

Testing


Automated application testing is a tough question. There are many approaches, and usually you have no choice but to choose a couple of them and try on your application written on the X. Play framework! takes 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 it’s easy to forget about, it is always convenient when it starts “somehow by itself”. In addition to testing automation, you can use the same interface during the development process (usually for this you have to configure the launch of tests from the IDE or ant / maven / shell scripts).



Quick turnaround


We at ZeroTurnaround (literally “zero turnover” or “instant response”) are obsessed with developer productivity. Our flagship product JRebel allows you to change the application code and just click the refresh button in your browser to instantly see your changes on the screen. Play Framework! offers similar functionality. I have not had enough time to study the source code of the Play in enough detail, but it seems that due to several small restrictions (some meta-information in bytecode, see tmp / bytecode / DEV, a stateless model, and a custom launcherplay run) It offers you a 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 tremendous advantage over other frameworks.

findings


Play Framework! offers cool chips, and these are only 5 of my favorites. Of course Play! - This is not a silver bullet, and it has its own problems, but this is the topic of a separate article. During the time that I have been working with Play !, it has proven to be a productive and intuitive environment for developing web applications. And what are your favorite pieces on Play !?




Toomas Römer is the co-founder and product lead of ZeroTurnaround, the same company that JRebel did . Thomas is a big fan of JUGs, OSS communities and beer. He writes blogs on dow.ngra.de , tweets as @toomasr, and also maintains the non-profit website chesspastebin.com . In his free time, he crashes Lexus at drive tests, plays chess, Go and Starcraft. His appearance is deceiving; he will surely make you squash. If anything, he and LinkedIn have an account.




Also popular now: