EIPinCPI – Scatter-Gather

December 13, 2019 Bhalchandra Wadekar

This week, we’ll study a routing pattern known as Scatter-Gather.

When do I use this pattern?

When the message needs to be sent to multiple recipients but only one response needs to be considered for further processing, then the Scatter-Gather pattern can be applied. For example, when booking a flight, the itinerary can be sent to multiple airlines for the quote, finally, only the best quote will be sent as a response.

Scatter-Gather in CPI

In CPI, Scatter part can be implemented by a Multicast, whereas, Gather part can be implemented by a combination of an Aggregator and a Groovy Script.

Integration Flow

In this integration flow, a user can place an order through the Online Ordering System (OS). The order is sent to ‘Airline1’ and ‘Airline2’ for a quote using Command Message Pattern. Finally, all the quotes are combined together using an Aggregator and the best Quote is determined using a Groovy Script.

Example Run

Let’s consider that the order arrives from the ordering system as follows:

<Order>
	<Id>1234</Id>
	<Flight>
		<From>United Kingdom</From>
		<To>India</To>
	</Flight>
</Order>

Airline 1 returns £500 total price, whereas, Airline 2 return £450 total price

<Price>
	<Id>1234</Id>
	<Airline>1</Airline>
	<Amount>500</Amount>
</Price>
<Price>
	<Id>1234</Id>
	<Airline>2</Airline>
	<Amount>450</Amount>
</Price>

The aggregator combines these two responses in one message like so:

<multimap:Messages xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">
    <multimap:Message1>
        <Price>
            <Id>1234</Id>
            <Airline>1</Airline>
            <Amount>500</Amount>
        </Price>
        <Price>
            <Id>1234</Id>
            <Airline>2</Airline>
            <Amount>450</Amount>
        </Price>
    </multimap:Message1>
</multimap:Messages>

Finally, the script keeps the cheapest price in the message:

<Price>
    <Id>1234</Id>
    <Airline>2</Airline>
    <Amount>450</Amount>
</Price>

Scatter-Gather vs Composed Message Processor

In Scatter-Gather, the entire input message is sent to various systems and the best response is selected. On the other hand, Composed Message Processor splits the message into sub-messages and combines the responses back into a single message.

Conclusion

The Scatter-Gather pattern is used when the message needs to be sent to multiple systems, however, only one response needs to be considered as output.

References/Further Readings

Hope this helps,
Bala

P.S.: To check how to implement Scatter-Gather pattern in SAP PO and learn another way of implementing Scatter-Gather pattern in SAP CPI, head over to Alexander Bundschuh‘s blog on Scatter-Gather pattern.

About the Author

Bhalchandra Wadekar

Bhalchandra Wadekar is an Integration Enthusiast who authored the Enterprise Integration Patterns in SAP Cloud Platform Integration (#EIPinCPI) series on SAP Blog Community. He is a fan of integration from API Management and Open Connectors to Message Based Integration and Data Integration.

More Content by Bhalchandra Wadekar
Previous Article
EIPinCPI – Event-Driven Consumer
EIPinCPI – Event-Driven Consumer

Next Article
EIPinCPI – Correlation Identifier
EIPinCPI – Correlation Identifier