Skip to content

Aggregator

Andres Castiglia edited this page May 29, 2017 · 1 revision

A Splitter is useful to break out a single message into a sequence of sub-messages that can be processed individually. Likewise, a RecipientList is useful to forward a request message to multiple recipients in parallel in order to get multiple responses to choose from. In most of these scenarios, the further processing depends on successful processing of the sub-messages. For example, we want to select the best bid from a number of vendor responses or we want to bill the client for an order after all items have been pulled from the warehouse.

How do we combine the results of individual, but related messages so that they can be processed as a whole?

Use a stateful filter, an Aggregator, to collect and store individual messages until a complete set of related messages has been received. Then, the Aggregator publishes a single message distilled from the individual messages.

The Aggregator is a special Filter that receives a stream of messages and identifies messages that are correlated. Once a complete set of messages has been received (more on how to decide when a set is 'complete' below), the Aggregator collects information from each correlated message and publishes a single, aggregated message to the output channel for further processing.

using Het.Common;

namespace Dummy
{
    [Component]
    public class Class4
    {
        [Aggregator("channel1", "channel2")]
        public void Run([Command] Command command)
        {
            command.Response.Message = string.Empty;

            var aux = new string[command.Length];

            for (int i=0; i<command.Length; i++) {

                aux[i] = command.Partial[i].Message;
            }

            command.Response.Message = string.Join(";", aux);
        }
    }
}
Clone this wiki locally