Mitch Pronschinske is a Senior Content Analyst at DZone. That means he writes and searches for the finest developer content in the land so that you don't have to. He often eats peanut butter and bananas, likes to make his own ringtones, enjoys card and board games, and is married to an underwear model. Mitch is a DZone Zone Leader and has posted 2576 posts at DZone. You can read more from them at their website. View Full User Profile

Grails With Terracotta: A Few Lines of Config

  • submit to reddit
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.


Steven Harris replied on Wed, 2010/05/05 - 12:33am

Nice blog! Thought the url reads a little funny :-)

Igal Levy replied on Wed, 2010/05/05 - 2:43pm

 Nice article. Just want to point out:

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

 This is wrong. Actually tcConfigUrl takes any kind of URL, for example "file:/my/path/tc-config.xml".

 tcConfig takes literal config (the content of tc-config.xml), for example "<tc:tc-config ... </tc:tc-config>".


Phuc Bui replied on Wed, 2010/05/05 - 9:25pm in response to: Steven Harris

Cool blog!

Matthew Passell replied on Fri, 2010/05/07 - 8:29pm in response to: Steven Harris

Agreed!  Hee hee.  It's like we're getting to read a leaked story. :)

Mitch Pronschinske replied on Mon, 2010/05/10 - 8:04am in response to: Steven Harris

Thanks.  I just took screenshots of the presentation instead of re-entering the code.  You can check out the presentation through one of the links in the text.  The url was generated when I put "don't publish" in the title, so that no one publishes it while it's in the moderation queue.

Tanya Huff replied on Thu, 2011/05/05 - 11:17pm

For those of you that haven't read Davids book 'Grails: A Quick-Start Guide' go grab it, it really helped me and my team. Singer Sewing Parts

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.