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!!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Tushar Paliwal
Tushar is a developer with experience in Groovy and Grails , Spring and enterprise Java Technologies.