Technical Articles
EIPinCPI – Request-Reply
Previous – Event Message | Index | Next – Return Address
This week, we are going to study one of the most used patterns known as Request-Reply.
When do I use this pattern?
In Messaging, the message travels from sender to receiver. However, if the sender expects a response, then the Request-Reply pattern is used. For example, getting an entity set from OData Service such as getting products from Northwind OData Service.
Request-Reply in Apache Camel
In Message Pattern, we learned that the message is set on an Exchange before sending it on a particular route. On the Exchange, the pattern can be set to InOut and Camel treats it as a Request-Reply pattern or synchronous pattern. Read more about this in Apache Camel’s documentation on Request-Reply. On the other hand, setting the pattern to InOnly results in Request only pattern or asynchronous pattern.
Request-Reply in CPI
In CPI, Request-Reply is implemented using a Request-Reply component. Let’s use it in CPI to get products from Northwind service.
Integration Flow
Steps
This integration flow starts immediately using the Timer Start component, gets products using OData Adapter, and Logs the output. Timer Start and Log script have been used previously, so I’ll include only the Get Products step.
Get Products
Get Products is a Request-Reply component and connects to the Receiver (Northwind in this case) using OData Adapter as Northwind exposes OData services.
These configurations for OData Adapter are changed from their default for this flow:
Tab | Parameter | Value |
Connection | Address | https://services.odata.org/V2/Northwind/Northwind.svc |
Processing | Resource Path | Products |
Processing | Query Options | $top=5 |
Output
When the integration flow is deployed, it requests Northwind to get top 5 Products and Northwind replies with 5 Products.
<Products>
<Product>
<CategoryID>1</CategoryID>
<Discontinued>false</Discontinued>
<SupplierID>1</SupplierID>
<UnitPrice>18.0000</UnitPrice>
<ProductName>Chai</ProductName>
<QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
<UnitsOnOrder>0</UnitsOnOrder>
<ProductID>1</ProductID>
<ReorderLevel>10</ReorderLevel>
<UnitsInStock>39</UnitsInStock>
</Product>
<Product>
<CategoryID>1</CategoryID>
<Discontinued>false</Discontinued>
<SupplierID>1</SupplierID>
<UnitPrice>19.0000</UnitPrice>
<ProductName>Chang</ProductName>
<QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
<UnitsOnOrder>40</UnitsOnOrder>
<ProductID>2</ProductID>
<ReorderLevel>25</ReorderLevel>
<UnitsInStock>17</UnitsInStock>
</Product>
<Product>
<CategoryID>2</CategoryID>
<Discontinued>false</Discontinued>
<SupplierID>1</SupplierID>
<UnitPrice>10.0000</UnitPrice>
<ProductName>Aniseed Syrup</ProductName>
<QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit>
<UnitsOnOrder>70</UnitsOnOrder>
<ProductID>3</ProductID>
<ReorderLevel>25</ReorderLevel>
<UnitsInStock>13</UnitsInStock>
</Product>
<Product>
<CategoryID>2</CategoryID>
<Discontinued>false</Discontinued>
<SupplierID>2</SupplierID>
<UnitPrice>22.0000</UnitPrice>
<ProductName>Chef Anton's Cajun Seasoning</ProductName>
<QuantityPerUnit>48 - 6 oz jars</QuantityPerUnit>
<UnitsOnOrder>0</UnitsOnOrder>
<ProductID>4</ProductID>
<ReorderLevel>0</ReorderLevel>
<UnitsInStock>53</UnitsInStock>
</Product>
<Product>
<CategoryID>2</CategoryID>
<Discontinued>true</Discontinued>
<SupplierID>2</SupplierID>
<UnitPrice>21.3500</UnitPrice>
<ProductName>Chef Anton's Gumbo Mix</ProductName>
<QuantityPerUnit>36 boxes</QuantityPerUnit>
<UnitsOnOrder>0</UnitsOnOrder>
<ProductID>5</ProductID>
<ReorderLevel>0</ReorderLevel>
<UnitsInStock>0</UnitsInStock>
</Product>
</Products>
Integration Recipe
The code is available as Integration Recipe on SAP’s official repository: EIP-MessageConstruction-Request-Reply
Conclusion
Request-Reply is one of the most used patterns. It is applied when the sender expects a reply.
References/Further Readings
- Request-Reply Pattern in Enterprise Integration Patterns
- Apache Camel’s documentation on Request-Reply
- CPI Components
Hope this helps,
Bala
Nice work, nowaday is not easy to take more information abou cpi and youa re doing a great job. Thank you for share you knowledg #keepwalking
Thank you Carlos Rodrigo.