This section explains, through an example scenario, how the Scatter-Gather EIP can be implemented using WSO2 ESB. The following topics are covered:
Table of Contents |
---|
...
Simulating the sample scenario
Use a SOAP client like SoapUI to send the following request to the
ScatterGatherProxy
service.Code Block language html/xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples"> <soapenv:Header/> <soapenv:Body> <ser:getSimpleQuote> <ser:symbol>Foo</ser:symbol> </ser:getSimpleQuote> </soapenv:Body> </soapenv:Envelope>
- Because the log mediator is enabled inside the outSequence, there will be three responses from the three vendors. The logs will be similar to the following:
- In SoapUI, you will get the response from the vendor providing the best quote as follows:
- Compare the logged response messages with the response received by the client to see that the
ScatterGatherProxy
service returns the best quote to the client.
How the implementation works
...
- clone [line 6 in ESB config] - In the
inSequence
of theScatterGatherProxy
service, we use the Clone mediator to make three copies of the request. Those requests are then forwarded to the three vendor services (SimpleStockeQuoteService
). The responses to those three requests are received at theoutSequence
. The Clone mediator is similar to the Splitter EIP. It clones the incoming request and passes the requests in parallel to several endpoints. . - log [line 25 in ESB config] - All received responses are logged before the Aggregate mediator merges them.
- aggregate [line 26 in ESB config] - The Aggregate mediator aggregates response messages for requests made by the Iterate or Clone mediator. The completion condition specifies the minimum or maximum number of messages to be collected.
- onComplete [line 30 in ESB config] - When all messages are aggregated, the sequence inside
onComplete
will be run. - In the
inSequence
of theScatterGatherProxy
service, we use the Clone mediator to make three copies of the request. Those requests are then forwarded to the three vendor services (SimpleStockeQuoteService
). The responses to those three requests are received at theoutSequence
. - All received responses are logged before the Aggregate mediator merges them. The
onComplete
sequence of the Aggregate mediatoronComplete
sequence of the Aggregate mediator will run. This sequence is called once all three responses are received or the specified completion condition is met. The responses are aggregated based on the value of thereturn
element in the response. - enrich [line 32 in ESB config] - The Enrich mediator is used to extract the response, which contains the best quote. The following XPath 1.0 expression is used for this purpose:
//m0:return[not(preceding-sibling::m0:return/m1:last <= m1:last) and not(following-sibling::m0:return/m1:last < m1:last)]
In essence, this message expression instructs the ESB to pick the response that has the lowestlast
value. (The XPath 2.0min
function could reduce the complexity of the above commandexpression, but XPath 1.0 is the current default supported by WSO2 ESB.) Once the proper response is found, we enrich the SOAP body with it and send that response back to the client.