(Quick Reference)

2 Quick Start - Reference Documentation

Authors: Bud Byrd

Version: 3.1.3

2 Quick Start

This is a quick and dirty how-to detailing how to quickly begin using the plugin. While there is a lot of configurability offered by this plugin, this is a very basic demonstration of its usage.

Create The Application

Create a project named RabbitExample. You can do this by entering:
grails create-app RabbitExample

Add The Plugin

In grails-app/conf/BuildConfig.groovy, under the plugins section, add:
plugins {
        // ...

compile name: "rabbitmq-native", version: "latest.release"

// … }

Then, refresh the project's dependencies:

grails refresh-dependencies

Configuring

In grails-app/conf/Config.groovy, add:
rabbitmq {
    connection = {
        connection host: "changeme", username: "changeme", password: "changeme"
    }
    queues = {
        queue name: "testqueue"
    }
}
Be sure to replace the appropriate values for your RabbitMQ server and user credentials.

Consumer

Create the a consumer by using the following command:
grails create-consumer com.example.Test

Update the consumer to reflect the following:

package com.example

import com.budjb.rabbitmq.consumer.MessageContext

class TestConsumer { /** * Consumer configuration. */ static rabbitConfig = [ queue: "testqueue" ]

/** * Handle an incoming RabbitMQ message. * * @param body The converted body of the incoming message. * @param context Properties of the incoming message. * @return */ def handleMessage(def body, MessageContext context) { println body return "Hello to you, too!" } }

Controller

Create a controller by using the following command:
grails create-controller com.example.Test

Update the controller to reflect the following:

package com.example

import com.budjb.rabbitmq.publisher.RabbitMessagePublisher

class TestController { RabbitMessagePublisher rabbitMessagePublisher

def index() { render rabbitMessagePublisher.rpc { routingKey = "testqueue" body = "Hello!" } } }

Run it!

Run the grails application.
grails run-app

You can see the application in action by hitting the test controller. If you're running this on your localhost, your URL may be similar to http://localhost:8080/RabbitExample/test/index. You should see the message "Hello!" printed the application's output console, and your web browser should dispay the message "Hello to you, too!"