Quantcast
Channel: Grails Info
Viewing all articles
Browse latest Browse all 45

Cloud Foundry + Grails + RabbitMQ – Publishing Messages into the Cloud

$
0
0

Having RabbitMQ in Cloud Foundry is great as I can now start using messaging between my applications running on Cloud Foundry. The next topic I would like to address is how can I publish a message from an application running outside of the cloud to an application running within Cloud Foundry?
In this blog I am going to walkthrough the steps involved in creating a Grails application that acts as a REST service that accepts an external JSON message and then publishes it onto my RabbitMQ broker running in Cloud Foundry.
You can try out my example at:
Rabbit Inbound Tracker Example: http://rabbit-inbound-tracker.cloudfoundry.com
Grails: Plugin Install
This is going to be a Grails application running on Cloud Foundry that publishes the body of a REST message on to RabbitMQ. The first thing to do is to install the Grails plugins for Cloud Foundry and RabbitMQ. Open the grails command console and execute:
install-plugin cloud-foundry
install-plugin rabbitmq
Grails: Rest Controller
Grails allows you to define a controller that acts as a REST endpoint. Let’s get Grails to create the controller so we can get started:
create-controller org.charris.example.Rabbit
Open the ‘RabbitController’ and a create the definition ‘publish’:
def publish = {
   
}
The first thing we are going to do within this definition is get hold of the JSON request body and send it over the RabbitMQ exchange ‘inbound’:
import grails.converters.*
def publish = {
    rabbitSend “inbound”, null, request.JSON.toString()   
}
This is really all the logic you need to do however we need to render a response back to the client so add ‘render’ to the end of the definition
def publish = {
    rabbitSend inbound, null, request.JSON.toString()   
    render request.JSON.toString()
}
Grails: Rest Configuration
At the moment we just have a controller so the next step is to map the appropriate REST URL to our  ‘publish’ logic.  In this example I map a POST on the URL “/api/rabbit/message’. To do this open the configuration file URLMapping and change the mapping as follows:
static mappings = {
    ”/api/rabbit/message (controller:rabbit){
        action = [POST: "publish"]
    }
    ”/” (view:”/index”)
}
Grails: Rabbit Configuration
The final step is to configure a connection factory and exchange within “/conf/Config.groovy”:
// grails-app/conf/Config.groovy
rabbitmq {
    connectionfactory {
        username = ‘guest’
        password = ‘guest’
        hostname = ‘localhost’
    }
    queues = {        
      exchange name: ‘inbound’, type: fanout, durable: false
    }
}
Grails: Publishing to the Cloud
The blog “Grails + RabbitMQ + Cloud Foundry: Messaging in Cloud Foundry” described how to deploy the publishing application to Cloud Foundry. To publish the rest controller execute:
cf-push
Testing
At this point you should have everything setup.
To test the service I am using curl:
curl –verbose –request POST –header “Content-Type: text/json” -d “{source:curl,name:chris}” http://rabbit-inbound.cloudfoundry.com/api/rabbit/message
If you have created your own inbound service then simple change the url to point to our endpoint.
I have implemented a message tracker that allows you to see the message you have sent into my service. 

Enjoy :-)

The post Cloud Foundry + Grails + RabbitMQ – Publishing Messages into the Cloud appeared first on Grails Info.


Viewing all articles
Browse latest Browse all 45

Trending Articles