Grails – A First Look

GrailsAFirstLookImages/grails_logo.jpg
Grails is a very interesting modern framework for the Java platform. As its name implies, it has built on the Ruby on Rails philosophy, but with an interesting twist. It is based on a very stable and popular stack; the Spring framework, Hibernate,SiteMesh, and Quartz. On top of this, it offers Groovy to produce a clean and simple interface to the stack, whilst remaining completely compatible with the JEE environment – the entire web application can be packaged as a war file.

Performance

If you use a web application framework that makes heavy use of a dynamic language such as Groovy, you're going to pay the price in performance, right? Well, yes, Grails will be slower than a 'pure' Java / Servlet / JSP solution. In fact a couple of Sun engineers have produced some benchmarks comparing Ruby on Rails, Grails and JEE.
Although a little old now, the results seem to indicate that Grails is between four and two times slower than JEE with between 100 and 500 concurrent users (page 33 of the PDF). Obviously, this may be worrying if you expect such high application usage, but if your application works in the 'long tail' region, say 10 to 100 concurrent users, then the figures seem acceptable – considering the performance increase in development efficiency.
Personally, I would have guessed performance ten times slower than JEE, so I'm actually quite happy. Grails is still at version 1.0, and there probably is some room for improvement in terms of efficiency.
Grails itself is about 70% Java code, and 30% Groovy. Although you can use Java in your own code, most of it will be in Groovy, due to the Groovy based interfaces in the model, view and controllers.

The Wow! Factors

Grails works very hard to produce a similar development environment (and development experience) as Ruby on Rails. It also makes clever use of Groovy to produce a very small footprint in terms of hand written code. The most positive wow! factors in my experience writing the WebAlbum application were in developing the tag library, and the security filters.
I have written a rather long article describing this development work in the Grails WebAlbum tutorial. Just to give you a small example of the tag library, this code extract from grails-app/taglib/WebAlbumTagLib.groovy:
class WebAlbumTagLib {

  static namespace = "wa"
...
    def pictureAnchor = { attrs, body ->
      def picture = attrs.remove('picture')
      def size = attrs.remove('size')
      out << "<a href=\"${createPictureLink(picture.id, size).encodeAsHTML()}\""
      attrs.each { key, value ->
        out << " $key=\"$value\""
      }
      out << '>'
      out << body()
      out << '</a>'
    }
...
together with this markup in grails-app/views/picture/show.gsp:
...
  <tr class="prop">
    <td valign="top" class="name">Caption:</td>
    <td valign="top" class="value">
      <wa:pictureAnchor picture="${picture}" size="${Image.Original}" title="Show Original" target="_blank">${picture.caption ?: '...'}</wa:pictureAnchor>
    </td>
  </tr>
...
produces the following XHTML snippet:
  <tr class="prop">
    <td valign="top" class="name">Caption:</td>
    <td valign="top" class="value">
      <a href="/image/7/1/7-Original.jpg" target="_blank" title="Show Original">One Man, but Three Hands!</a>
    </td>
  </tr>
It's interesting to note that I didn't have to write a Tag Library Descriptor file, and that the picture object in the GSP is 'passed through' to the pictureAnchor closure as is, so it can reference the properties (picture.id) immediately.
I didn't even have to stop the server to add pictureAnchor to the tag library, or to see the tag being used in the GSP.

Fast Prototyping

Similar to Ruby on Rails, Grails provides for very fast prototyping. It really can take just a few hours to get a basic application up and running. The major difference is that Grails, in the same way as Groovy, allows you to reuse all that hard earned JEE / Spring / Hibernate knowledge. For experienced Java programmers, Groovy and Grails is a far less traumatic transition than learning Ruby and Ruby on Rails.
Even if you can't, or won't, use Grails in your production environment, it's still worth investigating as a prototyping tool. Once the basic model has been ironed out, and all the difficult problems addressed, then you can transpose that model onto your preferred development environment, knowing that the model works. In addition, transposing a Groovy bean to a Java bean is relatively straightforward. Plus you've already got the Hibernate annotations worked out beforehand.

Overall Impressions

Groovy is a clean and powerful language, which provides a smooth and shallow learning curve. Grails is a clean implementation of a complex problem, which uses tried and trusted technologies. There are concerns raised about performance, but frankly, I'm prepared to pay that price given the dramatic development efficiency increase. Hardware will always remain cheaper than software development.
If you're new to both Groovy and Grails, you'll need a couple of weeks to get familiar with them. This is, however, a much shorter path than learning Ruby, and then Ruby on Rails, in my experience.
As with Ruby on Rails, I have found that the Grails learning curve is gradual. The most important thing to keep in mind is that “it has probably already been done”. Take time to check out the reference documentation and user guide – both of which are excellent.

Contacts

Syger can be contacted for consultancy work on any of the topics mentioned in this article, by sending an email to info@syger.it.

Valid CSS

Valid XHTML 1.0

Valid Atom 1.0