Tuesday 10 November 2009

Grails on GAE and persistence

Introduction
In my last post we created very simple Grails project and deployed it on Google Application Engine. Now we will try to extend it to use Google Datastore for persistence.

Configuration
Create a project in the same way as in previous post but ensure you will choose JPA as a persistence provider. Another option is JDO but I simply don't know this technology;)
Next step will be to install gorm-jpa plugin for Grails:
grails install-plugin gorm-jpa

Application goals
We will create very simple one page application to manage list of books. On the top there will be a list of book items. At the bottom there will a form to store new ones.



Implementation
We should create class for our domain object - grails-app\domain\test\Book.groovy:
package test

import javax.persistence.*;

@Entity
class Book implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id

String title

static constraints = {
id visible:false
}
}
We need also web controller that will be responsible for 3 web actions:
  • index - main action listing all books
  • save - adding new book
  • remove - removing book
Add following code to grails-app\controllers\BookController.groovy:
import test.*

class BookController {
def index = {
[books : Book.list( params )]
}

def save = {
Book b=new Book(params)
b.save()
redirect(action : index)
}

def remove = {
Book b=Book.get(params.id)
b.delete()
redirect(action : index)
}
}
Finally add GSP page (grails-app\views\book\index.gsp) that will present data and invoke particular actions of our controller:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
<title></title>
</head>
<body>
<h1>Books:</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th></th>
</tr>
</thead>

<g:each in="${books}" status="i" var="book">
<tr>
<td>${book.id}</td>
<td>${book.title}</td>
<td><g:link action="remove" id="${book.id}">remove</g:link> </td>
</tr>
</g:each>
</table>
<h1>New:</h1>
<g:form action="save">
<table>
<tr>
<td>Title</td>
<td><g:textField name="title" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" /></td>
</tr>
</table>
</g:form>
</body>
</html>
Deploy the application just as we did it in last post. Point the browser to /book resource of your web app. You should see our page running.

Now, you can also browse datastore on GAE account pages:

Saturday 7 November 2009

Deploying Grails application on Google Application Engine

Introduction
Some time ago it was announced that Google Application Engine supports Grails framework. As a great fan of Groovy and Grails I wanted to give it a try. I was also very curious to see how GAE works. In this article I would like to present simple "Hello World" project and its deployment process.

My example uses:
  • Grails 1.1.1 (http://www.grails.org)
  • GAE SDK 1.2.6 for Java (http://code.google.com/intl/pl/appengine/downloads.html#Google_App_Engine_SDK_for_Java)
  • Java 1.6
Configuration
As a first step please download required software and install it. To configure it properly you need to set up:
  • variable GRAILS_HOME to directory where you installed Grails
  • variable APPENGINE_HOME to directory where you installed GAE SDK
  • add GRAILS_HOME\bin directory to PATH variable
Creating GAE account
Go to GAE website (http://appengine.google.com) and create an account. Log in and add new application to your account. Specify application name - I will use "gae-grails-maciek-test".

Creating simple project
Go the your workspace directory and type: grails create-app GoogleGrailsTest
Script should create your project subdirectory - GoogleGrailsTest. You can import your project to Eclipse or another IDE.



Please modify GoogleGrailsTest\grails-app\conf\Config.groovy and add following line:
google.appengine.application="gae-grails-maciek-test"

Of course you should use your GAE application name. Go to project directory and install grails GAE plugin:
grails install-plugin app-engine

You will be asked if you want to use JPA or JDO for persistence. It is not important at all for our case.

To run you project locally on your computer type: grails app-engine run
You should be able to access it at http://localhost:8080



I must confess I noticed a strange stack trace in log files, but application was still running properly...

Deployment
Type:
grails set-version 1
grails app-engine package

First deployment should be done with:
$APPENGINE_HOME/bin/appcfg.cmd update ./target/war

Later when you just update application on GAE sever you can just type:
grails app-engine deploy

During deployment process you will be asked about your google account email and password. I noticed strange bug during authentication process. When script asks you for email just push Enter and go to the next line and enter it there. Password can be entered normally. Without this trick I was not able to authenticate properly.

Now you should be able to access your application at http://your-app-name.appspot.com - in my case on http://gae-grails-maciek-test.appspot.com/

Application can be monitored on GAE pages after logging in to your account: