public static final String BANK_QUOTE = "bank_quote";
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
// Get the bank quote instance from the exchange
BankQuote oldQuote = oldExchange.getProperty(BANK_QUOTE, BankQuote.class);
// Get the oldQute from out message body if we can't get it from the exchange
if (oldQuote == null) {
Object[] oldResult = (Object[])oldExchange.getOut().getBody();
oldQuote = (BankQuote) oldResult[0];
}
// Get the newQuote
Object[] newResult = (Object[])newExchange.getOut().getBody();
BankQuote newQuote = (BankQuote) newResult[0];
Exchange result = null;
BankQuote bankQuote;
if (newQuote.getRate() >= oldQuote.getRate()) {
result = oldExchange;
bankQuote = oldQuote;
} else {
result = newExchange;
bankQuote = newQuote;
}
// Set the lower rate BankQuote instance back to aggregated exchange
result.setProperty(BANK_QUOTE, bankQuote);
// Set the return message for the client
result.getOut().setBody("The best rate is " + bankQuote.toString());
return result;
}