My application is using Messaging to perform a Request-Reply.
How does a replier know where to send the reply?
The request message should contain a Return Address that indicates where to send the reply message.
This way, the replier does not need to know where to send the reply, it can just ask the request. If different messages to the same replier require replies to different places, the replier knows where to send the reply for each request. This encapsulates the knowledge of what channels to use for requests and replies within the requestor so those decisions do not have to be hard coded within the replier. A Return Address is put in the header of a message because it’s not part of the data being transmitted.
... Read the entire pattern in the book Enterprise Integration Patterns
Example: Golang ChannelsNEW
In Go, channels are first-class language constructs, making it easy to specify a channel as return address by including a field of type chan in the structure to be passed as the request:
type Request struct { data []int resultChan chan int }
A function handling requests can now publish responses to the channel provided in the Request structure:
func handle(queue chan *Request) { for req := range queue { req.resultChan <- sum(req.data) } }
Requestors can now specify the desired return channel with the request as in the following example:
reqChannel := make(chan *Request, 10) go handle(reqChannel) // Make two requests with separate return channels request1 := &Request{[]int{3, 4, 5}, make(chan int)} request2 := &Request{[]int{1, 2, 3}, make(chan int)} reqChannel <- request1 reqChannel <- request2 // Receive results on respective channels fmt.Printf("answer: %d %d\n", <-request1.resultChan, <-request2.resultChan)
Find the source for this code snippet on Github
Related patterns:
Correlation Identifier, Emerging Standards and Futures in Enterprise Integration, Message Channel, Messaging, Request-Reply