All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.samples.loanbroker.routers.BankQuotesResponseAggregator Maven / Gradle / Ivy

Go to download

The Loan Broker example application is based on the example presented in the Enterprise Integration Patterns book. This chapter of the book is available online so you can see a detailed description of the application here (http://www.eaipatterns.com/ComposedMessagingWS.html).

There is a newer version: 1.3.3
Show newest version
/*
 * $Id: BankQuotesResponseAggregator.java 3397 2006-10-02 22:21:36Z tcarlson $
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 *
 * The software in this package is published under the terms of the MuleSource MPL
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.samples.loanbroker.routers;

import java.util.Iterator;

import org.mule.config.i18n.Message;
import org.mule.impl.MuleMessage;
import org.mule.routing.inbound.EventGroup;
import org.mule.routing.response.ResponseCorrelationAggregator;
import org.mule.samples.loanbroker.LoanQuote;
import org.mule.umo.UMOEvent;
import org.mule.umo.UMOMessage;
import org.mule.umo.routing.RoutingException;
import org.mule.umo.transformer.TransformerException;

/**
 * BankQuotesAggregator receives a number of quotes and selects the
 * lowest.
 */
public class BankQuotesResponseAggregator extends ResponseCorrelationAggregator
{
    /**
     * This method is invoked if the shouldAggregate method is called and returns
     * true. Once this method returns an aggregated message the event group is
     * removed from the router
     * 
     * @param events the event group for this request
     * @return an aggregated message
     * @throws org.mule.umo.routing.RoutingException if the aggregation fails. in
     *             this scenario the whole event group is removed and passed to the
     *             exception handler for this componenet
     */
    protected UMOMessage aggregateEvents(EventGroup events) throws RoutingException
    {
        try
        {
            LoanQuote lowestQuote = null;
            LoanQuote quote = null;
            UMOEvent event = null;

            for (Iterator iterator = events.iterator(); iterator.hasNext();)
            {
                event = (UMOEvent)iterator.next();
                quote = (LoanQuote)event.getTransformedMessage();
                logger.info("Processing quote: " + quote);

                if (lowestQuote == null)
                {
                    lowestQuote = quote;
                }
                else
                {
                    if (quote.getInterestRate() < lowestQuote.getInterestRate())
                    {
                        lowestQuote = quote;
                    }
                }
            }

            logger.info("Lowest quote is: " + lowestQuote);
            return new MuleMessage(lowestQuote, event.getMessage());
        }
        catch (TransformerException e)
        {
            throw new RoutingException(Message.createStaticMessage("Failed to get lowest quote"),
                new MuleMessage(events), null, e);
        }
    }

    /**
     * Determines if the event group is ready to be aggregated. This is entirely up
     * to the application. It could be determined by volume, last modified time or
     * some other criteria based on the last event received.
     * 
     * @param events
     * @return
     */
    protected boolean shouldAggregate(EventGroup events)
    {
        return super.shouldAggregate(events);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy