Technical Articles
EIPinCPI – Correlation Identifier
Previous – Return Address | Index | Next – Message Sequence
This week, we’re going to study complementary pattern to Request-Reply known as Correlation Identifier.
When do I use this pattern?
In Messaging, a message flows from one participant to another. In Request-Reply, request message flows from sender to receiver and reply message flows from receiver to sender independent of each other. As reply is received independent of the request, how would sender know which request does the received reply is for?
This is resolved by the use of a Correlation Identifier. When sending the request, the sender adds a unique key to request. The receiver processes the request and puts the unique key as the Correlation Identifier in the reply message.
Using the analogy of emails, if you are responding to an email with the subject “Cutover Plan”, the reply email usually has the subject “Re: Cutover Plan”. The subject informs sender which email they have received the response for. If the response is received without a subject, then it is difficult to know what is the response for.
Correlation Identifier in CPI
To demonstrate the Correlation Identifier Pattern In CPI, we’ll tweak last week‘s example. When processing the customer we’ll set the Correlation Identifier to Customer’s Id. This way, when processing a response, we’ll know which customer the response is for.
Step 1: Enqueueing Customer
This step is exactly the same as last week.
Step 2: Processing Customer
In this step, we’ll add the Correlation Identifier by setting header ‘JMSCorrelationID’ as Customer’s Id. So, the Content Modifier step ‘Prepare Response Message’ has the following addition to the ‘Message Header’ tab:
Action | Name | Type | Data Type | Value | Default |
Create | JMSCorrelationID | XPath | String | /Customer/Id |
Step 3: Processing Response
In this step, we’ll add the Correlation Identifier when logging. So, the Groovy Script step ‘Log’ changes to:
import com.sap.gateway.ip.core.customdev.util.Message
def Message processData(Message message) {
def messageLog = messageLogFactory.getMessageLog(message)
messageLog.addAttachmentAsString('Response for Customer: ' + message.getHeader('JMSCorrelationID', String), message.getBody(String), null)
return message
}
Output
In monitoring, I can see the Customer Id when processing response is completed. In the screenshot below, processing of Customer ‘1’ is successful.
Conclusion
A Correlation Identifier is used to correlate reply with a request. The value of the Correlation Identifier needs to be agreed upon by sender and receiver. Usually, the value of the Correlation Identifier is the primary key of the business object being processed by the receiver.
References/Further Readings
- Correlation Identifier Pattern in Enterprise Integration Patterns
- CPI Components
- EIPinCPI
Hope this helps,
Bala