Grails, GDS and Gravity Step by Step Guide

This guide shows you how to configure Groovy and Grails to run with basic Gravity messaging over Granite Data Services.

The Grails version used was Grails 1.2.1. The gdsflex plugin version used was a 0.8 working version.

Note capitals denote symbolic references, not literal characters e.g. GRAILS-PROJECT-LOCATION might be C:\wspace\messaging. So here goes:

Create Grails Application

grails create-app messaging

Import it into STS. This isn't strictly necessary, but I always put the following logging config in grails-app/conf/Config.groovy right after log4j = {

 appenders { rollingFile name:'file', maxFileSize:'100KB', file:"${appName}.log" } root { info 'file' additivity = true } 

This will help you diagnose in case something isn't working.

Install GraniteDS Flex Plugin

If the gdsflex plugin version 0.8 still hasn't been released, checkout https://svn.codehaus.org/grails-plugins/grails-gdsflex/trunk. If in doubt use TortoiseSVN.

I checked out the HEAD version of the gdsflex plugin from SVN, which appears to be the 0.8 working version, because it has compatibility with Flex 4 and contains GDS 2.1. At the time of writing the version you get if you type grails install-plugin gdsflex is 0.7.2 and it didn't work for me with these latest bits of software.

If working with gdsflex version from SVN: At the end of BuildConfig.groovy add the line: grails.plugin.location.gdsflex = "GDSFLEX-PLUGIN-LOCATION"

Create file config/GraniteDSConfig.groovy with contents:

graniteConfig { gravityEnabled = true }

Configure Tomcat NIO Connector

Add the file scripts/_Events.groovy with contents:

import org.apache.catalina.connector.Connector;<br />
	eventConfigureTomcat = {<br />
	tomcat -> def ajpConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol") ajpConnector.port = 8009 ajpConnector.setProperty("redirectPort", "8443") ajpConnector.setProperty("protocol", "AJP/1.3") ajpConnector.setProperty("enableLookups", "false") tomcat.service.addConnector ajpConnector }

Configure Granite Services

Copy GDSFLEX-PLUGIN-LOCATION/src/granite/granite-config.xml to GRAILS-PROJECT-LOCATION/web-app/WEB-INF/granite/granite-config.xml.

Copy GDSFLEX-PLUGIN-LOCATION/src/flex/services-config.xml to GRAILS-PROJECT-LOCATION/web-app/WEB-INF/flex/services-config.xml.

This copying may not be necessary if you've installed the plugin with grails install-plugin.

Modify services-config.xml to add the messaging-service and the my-gravityamf channel described near the top of the GDS Data Push documentation page, but change {server.port} in the channel uri to 8009

Create the Flex Project

Create a project in Flash Builder called messaging (or whatever you like). Don't choose any application server type. Output folder: GRAILS-PROJECT-LOCATION/grails-app/views/swf.

Copy granite.swc and granite-essentials.swc to FLEX-PROJECT-LOCATION/libs.

Open Project properties > Flex Compiler > Additional compiler arguments and add the following: -services GRAILS-PROJECT-LOCATION/web-app/WEB-INF/flex/services-config.xml -context-root messaging -include-libraries libs/granite-essentials.swc.

Edit messaging.mxml (or whatever your main Flex class is called) and include the code at the top of the GDS Data Push documentation page along with some buttons to call the functions. Here's an example:

<s:application minheight="600" minwidth="955" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark"><fx:script>import mx.messaging.events.MessageEvent; import mx.messaging.messages.AsyncMessage; import mx.controls.Alert; import org.granite.gravity.Consumer; import org.granite.gravity.Producer; private var consumer:Consumer = null; private var producer:Producer = null; private function connect():void { consumer = new Consumer(); consumer.destination = "gravity"; consumer.topic = "discussion"; consumer.subscribe(); consumer.addEventListener(MessageEvent.MESSAGE, messageHandler); producer = new Producer(); producer.destination = "gravity"; producer.topic = "discussion"; } private function disconnect():void { consumer.disconnect(); consumer = null; producer.disconnect(); producer = null; } private function messageHandler(event:MessageEvent):void { var msg:AsyncMessage = event.message as AsyncMessage; trace("Received message: " + (msg.body as String)); lblReceived.text = msg.body as String; } private function send(message:String):void { var msg:AsyncMessage = new AsyncMessage(); msg.body = message; producer.send(msg); }</fx:script></s:application>

Lights, Camera, Action!

run grails run-app and visit http://localhost:8080/messaging/messaging.swf.

Click connect, type something into the text input and it should appear below. Now you're sending messages to yourself. You might want to send some anonymous valentines.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.