Date: Tue, 19 Mar 2024 03:14:09 +0000 (UTC) Message-ID: <256843636.54042.1710818049834@cwiki-he-fi.apache.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_54041_1697881293.1710818049833" ------=_Part_54041_1697881293.1710818049833 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The Scatter-Gather fr= om the EIP patterns allows you to route messages to a number of dynamically = specified recipients and re-aggregate the responses back into a single mess= age.
In this example we want to get the best quote for beer from several diff= erent vendors. We use a dynamic Recipient List to get the request for a quote to all vendors = and an Aggregator to p= ick the best quote out of all the responses. The routes for this are define= d as:
So in the first route you see that the Recipient List is looking at the listOfVendor=
s
header for the list of recipients. So, we need to send a message l=
ike
This message will be distributed to the following Endpoints: bean:vendor1
, be=
an:vendor2
, and bean:vendor3
. These are all beans which=
look like
and are loaded up in Spring like
Each bean is loaded with a different price for beer. When the message is=
sent to each bean endpoint, it will arrive at the MyVendor.getQuote<=
/code> method. This method does a simple check whether this quote request i=
s for beer and then sets the price of beer on the exchange for retrieval at=
a later step. The message is forwarded on to the next step using POJO Producing (see the @=
Produce annotation).
At the next step we want to take the beer quotes from all vendors and fi=
nd out which one was the best (i.e. the lowest!). To do this we use an Aggregator with a custom a=
ggregation strategy. The A=
ggregator needs to be able to compare only the messages from this parti=
cular quote; this is easily done by specifying a correlationExpression equa=
l to the value of the quoteRequestId header. As shown above in the message =
sending snippet, we set this header to quoteRequest-1
. This co=
rrelation value should be unique or you may include responses that are not =
part of this quote. To pick the lowest quote out of the set, we use a custo=
m aggregation strategy like
Finally, we expect to get the lowest quote of $1 out of $1, $2, and $3.<= /p>
You can find the full example source here:
camel-spring/src/test/java/org/apache/=
camel/spring/processor/scattergather/
camel-spring/src/test/resources/org/apache/camel=
/spring/processor/scattergather/scatter-gather.xml
You can lock down which recipients are used in the Scatter-Gather by usi= ng a static Recipient = List. It looks something like this
from("direct:= start").multicast().to("seda:vendor1", "seda:vendor2", "seda:vendor3"); from("seda:vendor1").to("bean:vendor1").to("seda:quoteAggregator"); from("seda:vendor2").to("bean:vendor2").to("seda:quoteAggregator"); from("seda:vendor3").to("bean:vendor3").to("seda:quoteAggregator"); from("seda:quoteAggregator") .aggregate(header("quoteRequestId"), new LowestQuoteAggregationStrategy= ()).to("mock:result")
A full example of the static Scatter-Gather configuration can be found i= n the Loan Broker= Example.
If you would like to use this EIP Pattern then please read the Getting Started, you may= also find the Architect= ure useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.