Part 2: Using Groovy, Grails and Gigaspaces “3G”

Part 2: Utilize a dynamic language, one that really anyone can learn.

I chose to use Groovy and Grails for this project.. why?

Because of Groovys natural support for the JAVA language anyone with a background in the language can be productive. In fact since Groovy is a dynamic language which means it supports first class functions, closures, etc… It saves a lot of the developers time. Groovy is not a statically typed language which means you don’t have to declare the “storage” type before you use the variable.. You can actually recast a variable depending on the problem you are working on making the langage very fluid. Groovy supports about 98% (i think) of native JAVA

Groovy benefits (http://groovy.codehaus.org/)

  • Is an agile and dynamic language for the Java Virtual Machine
  • Builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
  • Makes modern programming features available to Java developers with almost-zero learning curve
  • Supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain
  • Makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL
  • Increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
  • Simplifies testing by supporting unit testing and mocking out-of-the-box
  • Seamlessly integrates with all existing Java objects and libraries
  • Compiles straight to Java bytecode so you can use it anywhere you can use Java

Grails (http://www.springsource.com/products/grails) is an advanced and innovative open source web application platform that delivers new levels of developer productivity by applying principles like Convention over Configuration. Grails helps development teams embrace agile methodologies, deliver quality applications in reduced amounts of time, and focus on what really matters: creating high quality, easy to use applications that delight users.

Grails is built around SpringMVC. Groovy and GraIls behave like another web framework and dynamic language called Ruby on Rails…

So we are going to start now with Step 1., by creating the working directory used to develop the application.

This is accomplished using the grails helper script “create-app”

Step 1: Initialize the application container framework

gary-bergers-macbook-pro-2:~ gary$ grails create-app Cisco
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /Users/gary/Development/grails

Base Directory: /Users/gary
Running script /Users/gary/Development/grails/scripts/CreateApp_.groovy
Environment set to development
    [mkdir] Created dir: /Users/gary/Cisco/src
    [mkdir] Created dir: /Users/gary/Cisco/src/java
    [mkdir] Created dir: /Users/gary/Cisco/src/groovy
    [mkdir] Created dir: /Users/gary/Cisco/grails-app
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/controllers
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/services
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/domain
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/taglib
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/utils
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/views
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/views/layouts
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/i18n
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/conf
    [mkdir] Created dir: /Users/gary/Cisco/test
    [mkdir] Created dir: /Users/gary/Cisco/test/unit
    [mkdir] Created dir: /Users/gary/Cisco/test/integration
    [mkdir] Created dir: /Users/gary/Cisco/scripts
    [mkdir] Created dir: /Users/gary/Cisco/web-app
    [mkdir] Created dir: /Users/gary/Cisco/web-app/js
    [mkdir] Created dir: /Users/gary/Cisco/web-app/css
    [mkdir] Created dir: /Users/gary/Cisco/web-app/images
    [mkdir] Created dir: /Users/gary/Cisco/web-app/META-INF
    [mkdir] Created dir: /Users/gary/Cisco/lib
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/conf/spring
    [mkdir] Created dir: /Users/gary/Cisco/grails-app/conf/hibernate
[propertyfile] Creating new property file: /Users/gary/Cisco/application.properties
     [copy] Copying 1 resource to /Users/gary/Cisco
    [unjar] Expanding: /Users/gary/Cisco/grails-shared-files.jar into /Users/gary/Cisco
   [delete] Deleting: /Users/gary/Cisco/grails-shared-files.jar
     [copy] Copying 1 resource to /Users/gary/Cisco
    [unjar] Expanding: /Users/gary/Cisco/grails-app-files.jar into /Users/gary/Cisco
   [delete] Deleting: /Users/gary/Cisco/grails-app-files.jar
     [move] Moving 1 file to /Users/gary/Cisco
     [move] Moving 1 file to /Users/gary/Cisco
     [move] Moving 1 file to /Users/gary/Cisco
Installing plug-in hibernate-1.1.1

Step 2. Setup POJO based on Person

Notice we can use JAVA 5 annotations here which reinforces convention over configuration and DRY programming.

The @SpaceClass tells Gigaspaces where to map the values.

We generate a unique id UUID as an index into our record

import java.util.UUID
import com.gigaspaces.annotation.pojo.*;

@SpaceClass
class Person  {
UUID id
String firstName
String lastName
    static constraints = {
    }
  static mapping = {
  }

}

Step 3. Next we need to create the Singleton which will get us a handle to the Space. We do this by setting up the following method in the services directory.

I call this method InitSpaceService.groovy

import org.openspaces.core.GigaSpace;
import org.openspaces.core.GigaSpaceConfigurer;
import org.openspaces.core.space.UrlSpaceConfigurer;
import com.j_spaces.core.IJSpace;

class InitSpaceService {

    boolean transactional = false

    def initSpace(String s) {

IJSpace space = new UrlSpaceConfigurer("jini://*/*/${s}").space()
GigaSpace gigaSpace = new GigaSpaceConfigurer(space).gigaSpace()
return gigaSpace

    }
}

Step 4. Setup the controller

Now to gain access to the methods in service we merely define it with the following line under the class definition [PersonController]

class PersonController {

def initSpaceService



def list = {
	GigaSpace sp = initSpaceService.initSpace("space")
	flash.message = "Space Count " + sp.count(new Person())
	Person[] results = sp.readMultiple(new Person(), Integer.MAX_VALUE);
    	[ personInstanceList: results, personInstanceTotal: sp.count(new Person()) ]
}

def show = {
	GigaSpace sp = initSpaceService.initSpace("space")
	UUID id = UUID.fromString(params.id);
	def personInstance = new Person()
	personInstance.id=id
	def record = sp.read(personInstance)
        if(!personInstance) {
            flash.message = "Person not found with id ${params.id}"
            redirect(action:list)
        }
        else { 
				return [ personInstance : record ]
		}
}

def delete = {
	GigaSpace sp = initSpaceService.initSpace("space")
	UUID id = UUID.fromString(params.id);
	def personInstance = new Person()
	personInstance.id=id
	def record = sp.clear(personInstance)
	flash.message = "Person ${params.id} deleted"

	redirect(action:list)
}

.. other controller instances left out..
}

Other steps necessary are to remove hibernate with the following:

grails uninstall-plugin hibernate

in Part3: We will go over how to deploy this application on a Gigaspaces fabric over Amazon EC2 clouds US-EAST and EU-West tying them together across an SSL overlay.