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
Ingrails-app/conf/BuildConfig.groovy
, under the plugins
section, add:
plugins { // ... compile name: "rabbitmq-native", version: "latest.release" // … }
grails refresh-dependencies
Configuring
Ingrails-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
package com.exampleimport com.budjb.rabbitmq.consumer.MessageContextclass 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
package com.exampleimport com.budjb.rabbitmq.publisher.RabbitMessagePublisherclass TestController { RabbitMessagePublisher rabbitMessagePublisher def index() { render rabbitMessagePublisher.rpc { routingKey = "testqueue" body = "Hello!" } } }
Run it!
Run the grails application.grails run-app
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!"