KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > loanbroker > routers > BankQuotesResponseAggregator


1 /*
2  * $Id: BankQuotesResponseAggregator.java 3397 2006-10-02 22:21:36Z tcarlson $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.samples.loanbroker.routers;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.mule.config.i18n.Message;
16 import org.mule.impl.MuleMessage;
17 import org.mule.routing.inbound.EventGroup;
18 import org.mule.routing.response.ResponseCorrelationAggregator;
19 import org.mule.samples.loanbroker.LoanQuote;
20 import org.mule.umo.UMOEvent;
21 import org.mule.umo.UMOMessage;
22 import org.mule.umo.routing.RoutingException;
23 import org.mule.umo.transformer.TransformerException;
24
25 /**
26  * <code>BankQuotesAggregator</code> receives a number of quotes and selects the
27  * lowest.
28  */

29 public class BankQuotesResponseAggregator extends ResponseCorrelationAggregator
30 {
31     /**
32      * This method is invoked if the shouldAggregate method is called and returns
33      * true. Once this method returns an aggregated message the event group is
34      * removed from the router
35      *
36      * @param events the event group for this request
37      * @return an aggregated message
38      * @throws org.mule.umo.routing.RoutingException if the aggregation fails. in
39      * this scenario the whole event group is removed and passed to the
40      * exception handler for this componenet
41      */

42     protected UMOMessage aggregateEvents(EventGroup events) throws RoutingException
43     {
44         try
45         {
46             LoanQuote lowestQuote = null;
47             LoanQuote quote = null;
48             UMOEvent event = null;
49
50             for (Iterator JavaDoc iterator = events.iterator(); iterator.hasNext();)
51             {
52                 event = (UMOEvent)iterator.next();
53                 quote = (LoanQuote)event.getTransformedMessage();
54                 logger.info("Processing quote: " + quote);
55
56                 if (lowestQuote == null)
57                 {
58                     lowestQuote = quote;
59                 }
60                 else
61                 {
62                     if (quote.getInterestRate() < lowestQuote.getInterestRate())
63                     {
64                         lowestQuote = quote;
65                     }
66                 }
67             }
68
69             logger.info("Lowest quote is: " + lowestQuote);
70             return new MuleMessage(lowestQuote, event.getMessage());
71         }
72         catch (TransformerException e)
73         {
74             throw new RoutingException(Message.createStaticMessage("Failed to get lowest quote"),
75                 new MuleMessage(events), null, e);
76         }
77     }
78
79     /**
80      * Determines if the event group is ready to be aggregated. This is entirely up
81      * to the application. It could be determined by volume, last modified time or
82      * some other criteria based on the last event received.
83      *
84      * @param events
85      * @return
86      */

87     protected boolean shouldAggregate(EventGroup events)
88     {
89         return super.shouldAggregate(events);
90     }
91
92 }
93
Popular Tags