Feb
14
2010
0

Serving Grails static content with UI-Performance and Cloud Foundry

I am excited about Cloud Foundry. It promises to greatly simplify java application deployment to Amazon’s Elastic Compute Cloud (EC2). With a few clicks, you can deploy an J2EE application (e.g. Grails) to the container of your choice (Tomcat 5.5tc Server, and more) on a fresh EC2 Linux instance backed by a MySQL database on Amazon’s Elastic Block Store (EBS). It doesn’t get much easier than this. The instance configuration automatically configures your container behind an Apache instance, which gives you some great options for hosting static content or configuring side-by-side application platforms.

I’ve been using Burt Beckwith’s UI Performance plugin for Grails to bundle and compress my static content (images, js, css) during the build, so I can save on outbound bandwith and improve the response time of my application. In order to take advantage of this build-time optimization, we need to modify the Apache configuration to serve static content instead of proxying those requests to the container.

  1. $ grails install-plugin ui-performance
  2. Configure your app to use the plugin’s taglibs and to bundle resources if necessary. (See http://www.grails.org/plugin/ui-performance)
  3. Add a handler for the CreateWarEnd event in your scripts/Events.groovy file (make one if you don’t have one). This handler will package all the bundled and gzipped resources into a static zip file that you can upload to Cloud Foundry:
    eventCreateWarEnd = {warName, stagingDir ->
        def directory = new File(warName).parent
    
        ant.zip(destfile:"${directory}/static.zip") {
            fileset(dir:"${stagingDir}") {
                include name: "images/**"
                include name: "css/**"
                include name: "js/**"
            }
        }
    }
    
  4. Create a “container initialization script” for Cloud Foundry to execute when your instance starts up. This script will tell Apache to directly serve the following directories and to add the appropriate Content-Encoding header for content that is already gzipped. The name doesn’t really matter at this point, but I called mine proxyexcludes.sh.
    cat > /etc/httpd/conf.d/_proxyexcludes.conf <<END
    ProxyPass /images !
    ProxyPass /css !
    ProxyPass /js !
    
    SetEnvIf Request_URI "\\.gz\\.(css|js|jpg|gif|png|ico)\$" already_compressed=true
    Header set Content-Encoding gzip env=already_compressed
    END
    

    I found that keeping this file up to date was easier if I just let grails rebuild it every time after the WAR is created. I modified my eventCreateWarEnd handler to take care of this for me:

    eventCreateWarEnd = {warName, stagingDir ->
        def directory = new File(warName).parent
    
        ant.zip(destfile:"${directory}/static.zip") {
            fileset(dir:"${stagingDir}") {
                include name: "images/**"
                include name: "css/**"
                include name: "js/**"
            }
        }
    
        ant.echo "Creating Proxy exclusion list: ${directory}/proxyexcludes.sh"
        new File("${directory}/proxyexcludes.sh") << """cat > /etc/httpd/conf.d/_proxyexcludes.conf <<END
    ProxyPass /images !
    ProxyPass /css !
    ProxyPass /js !
    
    SetEnvIf Request_URI "\\.gz\\.(css|js|jpg|gif|png|ico)\$" already_compressed=true
    Header set Content-Encoding gzip env=already_compressed
    END
    """
    
    }
    
  5. $ grails war
  6. Upload the static.zip file produced during the build as “static content” when you upload your app to Cloud Foundry:

    Cloud Foundry Upload Static Content

  7. When creating a deployment, upload your “proxyexcludes.sh” container initialization script:

    Cloud Foundry Deployment

  8. Fire up your instance and check out the performance gains!
Oct
27
2009
2

Grails GORM and UUIDs

Let’s say that you’ve decided you want to add a globally-unique identifier (GUID/UUID) to one of your domain classes. You might want to do this for portability to other systems or for a variety of reasons. Given that some people in the Grails community don’t recommend using a UUID or string as a primary key, you might find that you want to add a UUID as a separate natural key to your class and set it using GORM’s beforeInsert event hook:

class Person {
    String name
    String uniqueIdentifier

    transient beforeInsert = {
        uniqueIdentifier = java.util.UUID.randomUUID().toString()
    }
}

This works great, but if you’re like me, in that you like to work with test data while developing, you probably have some code in your BootStrap.groovy file that no longer works. The problem is that GORM’s validate() method does not call event hooks, and that property we just added is not nullable. One small change and we’re back in business:

class Person {
    String name
    String uniqueIdentifier

    stating constraints = {
        uniqueIdentifier(nullable:true)
    }

    transient beforeInsert = {
        uniqueIdentifier = java.util.UUID.randomUUID().toString()
    }
}
Written by Dan in: Grails |

Powered by WordPress | Theme: Aeros 2.0 by TheBuckmaker.com