Monday, 8 August 2011

RabbitMQ Publisher Subscribe Example in Java on Ubuntu

To install RabbitMQ on Ubuntu is as simple as executing:

sudo apt-get install rabbitmq-server

Next we need to download the java client libraries as they are not included in the ubuntu package. You can find the libraries at Unzip the archive and include the jars in your classpath

First we need a way of creating connections to RabbitMQ and also a method to setup exchanges, queues and routing keys: Run the main method to setup our test queue.

Then create the publisher:

Then the consumer

The execute the main method on the Publisher and Consumer and watch the messages flow.

Sunday, 7 August 2011

Free AI Course From Stanford - Introduction to Artificial Intelligence

Standford university's Sebastian Thrun and Peter Norvig in the next session of CS221: Introduction to Artificial Intelligence (October 2011) are throwing open the doors and allowing anyone, anywhere enrolment into this excellent course. All materials will be available online and there will be a homework assignment each week along with a mid-term and final exam. The best thing is that its free, to as many people who sign up.

Registration is occurring soon so sign up here at

There is a text book associated with the course. This text is used in over 1200 universities to teach AI so will be an asset for any bookshelf.

Tuesday, 26 July 2011

Grails Accessing Raw XML from a Request

There have been times in a grails controller I've wanted direct access to the xml in a POST request. You can retreive this via the request object: request.reader.text But what if you need access the raw xml and then to also use the xmlslurper to pick apart the xml. Accessing the request.XML after request.reader.xml throws this exception: java.lang.IllegalStateException: getReader() has already been called for this request

Say I have the following xml:

and I want to access the raw xml tag 'three' but I want programmatic access to the tag 'two'. The following allows that:

Monday, 18 July 2011

Grails Script to Copy Jar Dependencies

The usual way to include jar dependencies in Grails is to set the versioned dependencies in the BuildConfig.groovy. One issue I can see with this is that it forces you to use Maven, Ivy or at the very least a directory structure that is versioned. While this is a good practice and should be encouraged, there are times when you have to work with legacy build systems and you dont have the benefit of good structure. A script such as the following can copy the jars into the lib directory of the grails project. Call it something like Copyjars.groovy and put it in the scripts directory in the root of the grails project.

Then run the script using "grails prod copyjars"

Friday, 15 July 2011

Grails Jar'd Configuration Property Files

Say you have an existing Java project which has a plethora of configuration property files and you want to use some of these properties in your new Grails project. How can you do this? One approach is to:
  1. Put all your property files into a jar and place the jar in the 'lib' directory of your grails project
  2. Modify the Config.groovy and add the line -
    grails.config.locations = [ "", "" ]
  3. Then depending where you are in the app you have access to the property variables and can dereference them e.g. ${my.file1.prop}