How to send RPC REMOTE PROCEDURE CALL using Rabbitmq Native Grails Plugin

Posted By : Tushar Paliwal | 04-Sep-2015

Publishing an RPC message is as easy as sending messages In publisher side :-

import com.budjb.rabbitmq.publisher.RabbitMessagePublisher
class DemoPublisher {
    RabbitMessagePublisher rabbitMessagePublisher

    def sendSomeMessage() {
        def result = rabbitMessagePublisher.rpc {
            exchange = "task"
            routingKey ="cutycapt."+message.timelineEntry.id
            body = "This is a RPC call"
            timeout = 7000
        }
    }
}

In the above example publisher send an message type as rpc to the consumer ,In consumer it will match with its routing key and process the task and repond back to publisher. Publisher wait for reponse from consumer for 7 seconds because we defined time in this timeout variable, you can increase or decrease timeout as your requirnment. If response not come within 7 seconds than it will throw timeout exception In consumer side:- you can simply return response as like this:-

class RpcExampleConsumer {
 static rabbitConfig = [
	"queue": "cutycapt",
        "binding": "cutycapt.#"
    ]
    def handleMessage(String message, MessageContext messageContext) {
        println "received ${message}"
        return "response" // this message will be sent back to the consumer via its replyTo queue
    }
}

Or you can use rabbitMessagePublisher to respond like this:-


class RpcDemoConsumer {
    def rabbitMessagePublisher
	 static rabbitConfig = [
	"queue": "cutycapt",
        "binding": "cutycapt.#"
    ]
    def handleMessage(String message, MessageContext messageContext) {
        println "received ${message}"

        rabbitMessagePublisher.send {
            routingKey: messageContext.properties.replyTo
            body: "response from consumer"
        }
    }
}

In this way you can send an RPC type messages.

Thanks!!

About Author

Author Image
Tushar Paliwal

Tushar is a developer with experience in Groovy and Grails , Spring and enterprise Java Technologies.

Request for Proposal

Name is required

Comment is required

Sending message..