Suppose for a moment that this is not a terrible idea to implement RPC over message queues (e.g. RabbitMQ)
This is not terrible! it is common and recommended in many situations - not just inherited integration.
... ok, your real question now :)
in terms of a very high level, here is what you need to do.
Your request and response should have two key pieces of information:
- a
correlation-id
- a
reply-to
queue
These bits of information will allow you to adjust the original request and response.
Before submitting a request
request a code to create your own queue. This queue will be used to receive responses.
Create a new correlation identifier โ usually a GUID or UUID โ to guarantee uniqueness.
When sending a request
Attach the correlation ID that you created to the message properties. there is a correlationId
property that you should use for this.
save the correlation identifier with the corresponding callback function (response handler) for the request, somewhere inside the code making the request. you will need it when the answer comes.
attach the name of the created exclusive queue to the replyTo
property of the message.
with all this, you can send a message via rabbitmq
in response
The response code should use both correlationId
and replyTo
fields from the original message. so be sure to grab these
The response must be sent directly to the replyTo
. Do not use standard publication through the exchange. instead, send the response message directly to the queue using the send to queue function of any library you use, and send the response directly to the replyTo
.
be sure to include correlationId
in your response. This is an important part to answer your question.
when processing the response
The code that made the original request will receive a message from the replyTo
. it will pull correlationId
from message properties.
use the correlation identifier to find the callback method for the request ... code that processes the response. pass a message to this callback method, and you pretty much did.
implementation details
it works from a high level point of view. when you enter the code, implementation details will vary depending on the language and driver / library used.
most of the good RabbitMQ libraries for any given language will have a request / response built into it. If you do not, you may want to find another library. If you are not writing a template-based library on top of the AMQP protocol, you should look for a library that has common templates implemented for you.
If you need more information about the Request / Reply template, including all the details that I have provided here (and much more), check out these resources:
If you work in Node.js, I recommend using the wascally library, which includes the Request / Reply function you need. For Ruby check out bunny . For Java or .NET, consider some of the many service bus implementations. In .NET, I recommend NServiceBus or MassTransit.