Sunday, June 13, 2010

Configuring a Grails App for Clustering using Ehcache

Clustering Grails using Ehcache is very easy. Here is how to do it in just a few simple steps.

Requirements:
1) Grails 1.3.1 installed. Download Grails here if you don't already have it installed.
2) Terracotta 3.2.1 installed. Download Terracotta here if you don't already it installed.

This post assumes you have Grails installed to $GRAILS_HOME and Terracotta installed to $TERRACOTTA_HOME.

Before setting up your application for clustering, we'll need a Grails app. If you don't already have a Grails app, let's create one. We just repeat the steps listed on the Grails Quick Start Page. I will create a simple application called "Events" which create and stores events:

Step 1. Create the application

$ grails create-app events

Step 2. Create a domain class

grails create-domain-class Event

Edit the generated Event domain class grails-app/domain/events/Event.groovy and add some fields to it:

package events
class Event {
Date date
String title
}

Step 3. Create a controller

$ grails create-controller Event

Edit the grails-app/controllers/events/EventController.groovy to implement default scaffolding:

package events

class EventController {
def scaffold = Event
}

Step 4. Run the app

$ grails run-app

And browse to http://localhost:8080/events/event

Now we have a complete Grails application. Let's add Terracotta:

Step 5. Configure your domain class for caching

You will need to tell Hibernate that your domain class is cacheable. Edit the domain class at grails-app/domain/events/Event.groovy and add the cache directive:

package events

class Event {
static mapping = {
cache true
}

Date date
String title
}

Step 6. Configure Grails to use the latest version of Ehcache with Terracotta support built-in

Edit the config file at grails-app/conf/BuildConfig.groovy. Update the section which imports the default global settings like so to update the depencencies to the latest version of Ehcache (sidenote, Ehcache version 2.1.0 depends on Terracotta version 3.2.1, don't get confused by the version numbers - they don't line up because the two products are different even if owned by the same company):

grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits( "global" ) {
// uncomment to disable ehcache
// excludes 'ehcache'
runtime 'net.sf.ehcache:ehcache-core:2.1.0'
runtime 'net.sf.ehcache:ehcache-terracotta:2.1.0'
}

<rest of file here>

Step 7. Configure Ehcache to use Terracotta.

By default Ehcache caches are not configured for Terracotta support. Enable this by overriding the built-in Ehcache defaults by adding the file grails-app/conf/ehcache.xml with the following contents:

<ehcache name="EventCache">
<defaultCache
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false">
<terracotta/>
</defaultCache>
&;t'terracottaConfig url="localhost:9510"/>
</ehcache>

Step 8. Start a Terracotta Server

Terracotta requires that a Server is running. Start it now:

$ $TERRACOTTA_HOME/bin/start-tc-server.sh

Step 9. Start a developer console

To observe caching in action, start a Terracotta Developer Console:

$ $TERRACOTTA_HOME/bin/dev-console.sh

Step 10. Run the app again

$ grails run-app

You can monitor the cache stats in the Terracotta Developer Console. To do so, make sure you turn on statistics gathering:
  1. Click on Ehcache
  2. Click on Statistics
  3. Find the "Enable Statistics" button and click it

The following screen shot shows where to click:



Now, navigate to the application at http://localhost:8080/events/event. Create an event. After creating the event, you are left on a page that views the event. Press "Refresh" on your browser a few times, and notice that you get activity in the Developer Console Statistics window.

Here's what it should look like:



That's it - have fun with clustered Ehcache for Grails!

15 comments:

tunggad said...

Hey, this post does not matter with clustering grails app (session clustering). Its only about configuring grails to use distributed (with Terracotta) EHCache as Hibernate 2nd Level Cache.

曾法幸 said...

人生有些波折,才能有些成長,所以不論順逆,凡是成長、成功的助緣,都應該心存感激。..................................................

瑛萍 said...

時間就是塑造生命的材料。

偉曹琬 said...

好的部落格就要和好朋友分享--感謝分享..................................................

凱許倫 said...

教育的目的,不在應該思考什麼,而是教吾人怎樣思考......................................................................

佳張張張張燕張張張張張 said...

感覺很好的blog,祝你開心喔............................................................

靜蔡蔡蔡蔡怡 said...

好的部落格就要和好朋友分享--感謝分享............................................................

黃英吳思潔吳思潔邦 said...

時間就是塑造生命的材料。

偉DimpleHolloway043昀 said...

卡爾.桑得柏:「除非先有夢,否則一切皆不成。」共勉!.................................................................

Marco Vermeulen said...

Awesome post, thanks very much. Easy and to the point.

Jianfeng Tian said...

Hi,

Thanks for the post. I am pretty new to the clustering thing.

Just wonder when using terracotta, do you still need to create clusters in tomcat according to the Tomcat clustering doc: http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html

Many thanks

mrknaughty said...

Thanks for the info
mrknaughty

Nike shox shoes said...

Your articles are very inspiring indeed. I keep coming back! There are so many things I agree on. I hope we can exhange ideas in the future.

Cheap Nike shox can surely not let you down,romance yourself with nike shox sale

aa said...

As I write this post—longhandOffice 2010in a spiral notebook—I’m 20,000 feet above eastern Washington, having Microsoft Office 2010just crossed above the Cascades on my return flight Microsoft wordto Chicago. I visited Seattle for the weekend to Office 2007and I have known each other for 20 years now. They Microsoft Officehad a lovely ceremony, and the trip in general was fantastic.Microsoft Office 2007In the 13 years since I left Seattle, I’ve Office 2007 keyvisited six or seven times, and I always return to wherever has Office 2007 downloadOffice 2007 Professionalbecome home with mixed feelings about the place. It Outlook 2010both alarms and pleases me to see howMicrosoft outlookthat once-familiar areas seem almost foreign. ForMicrosoft outlook 2010neighborhoods have changed, to the point Windows 7 as have cookie-cutter, here-today-and-gone-tomorrow nightclubs that cater to the shiny shirt crowd.

sweet said...

supra shoes
cheap supra shoes
supra skytop
supra cruizer