Over a million developers have joined DZone.
Platinum Partner

Grails With Terracotta: A Few Lines of Config

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Dave Klein, the author of "Grails: A Quick-Start Guide" and Mike Allen, the Terracotta Product Manager, recently presented a seminar entitled "Clustering and Scaling Grails Apps: A Simple Approach."  Using an example application from "Grails: A Quick Start Guide," Klien shows us how to get a Grails application running with Terracotta and cluster a Grails app with Web Sessions Express.  Allen explains that Terracotta is known for scaling apps out to multiple nodes without overloading the database or losing application data consistency between nodes.  Grails already uses many of the technologies that Terracotta implements, including Quartz, Ehcache, and Hibernate.  For this reason, Allen says a lot of Grails users loved the Ehcache 2.0 beta (codenamed 'Darwin').  With each of those tools, newly released versions of Terracotta (3.2.1) and Ehcache (2.0) are now pluggable by config only.  Terracotta runs transparently behind Grails' core tools (Hibernate Ehcache, and Quartz), which are used to rapidly build web apps.  Klein thinks that tools like Terracotta and Grails are great because there are low barriers to entry.  When he first encountered Terracotta, he was impressed with how easy it is to use with Grails. 

Klein walks through the steps for running Grails with Terracotta.  First, he copies the Terracotta .jar into his Tomcat libraries.  After that he only needs to create one configuration file.  Klien starts by creating a context.xml file in his META-INF directory.  Next he finds the application, called Tekdays.  It's a web application for organizing and finding technology events.  Klien starts by checking out the Tekdays source code from the final chapter of his book and updating it to the most recent version of Grails.  He first shows how to integrate web sessions express from Terracotta using a few lines of XML config:

As an alternative to the tcConfigUrl, you can also drop the "Url" and put a file name in there.

Next, he modifies a few classes in his application and makes them serializable.  First is SecurityFilters.groovy:


He also modifies TekUser.groovy.  In a previous blog, Klien says he modified all the domain classes, but only these two classes are really necessary for this example:


TekUser.groovy gets stored in the session, so as you cluster things you want that to be available.  As you do more things, it might make more sense to modify all the domain classes.

That's all the config required!  Next you'll build your WAR.  Klien had one already prepared and started up his Tomcat servers next.  

On the two Tomcat instances, Klien showed that an entry's data in Tekdays was saved as he changed from one instance to the other, showing how the failover would work.  

If you want to cache your domain class using Ehcache, all you have to do is add a mapping block:

Obviously, Klein says that there are more options, but for the most part, running a Grails app with Terracotta is a simple process.  Check out the web seminar to see how Klein uses the Quartz job scheduler and other options.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}