Enterprise Integration Patterns
Messaging Patterns
HOME PATTERNS RAMBLINGS ARTICLES TALKS DOWNLOAD BOOKS CONTACT
Messaging Patterns
Return AddressReturn AddressMessaging Patterns » Message Construction

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


Creative Commons Attribution License

You can reuse the following elements under the Creative Commons Attribution license: pattern icon, pattern name, problem and solution statements (in bold), and the sketch. Other portions are protected by copyright.

Enterprise Integration Patterns book cover

Enterprise Integration Patterns
The de-facto language for designing asynchronous, distributed systems. Over 100,000 copies sold.

Software Architect Elevator book cover

The Software Architect Elevator
Rethink the role of architects as a connecting element across organizational layers. Acquire the technical, communication, and organizational skills to succeed in this new role.

Cloud Strategy book cover

Cloud Strategy
Make your cloud migration a success by translating high-level goals into conscious decisions with well-understood trade-offs.

Platform Strategy book cover

Platform Strategy
Platforms can boost innovation through harmonization, but they aren't easy to build. Learn from over a decade of designing and rolling out IT platforms.