001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.processor;
018
019 import org.apache.camel.Expression;
020 import org.apache.camel.Predicate;
021 import org.apache.camel.Processor;
022 import org.apache.camel.processor.aggregate.AggregationCollection;
023 import org.apache.camel.processor.aggregate.AggregationStrategy;
024 import org.apache.camel.processor.aggregate.DefaultAggregationCollection;
025 import org.apache.camel.processor.aggregate.PredicateAggregationCollection;
026
027 /**
028 * An implementation of the <a
029 * href="http://camel.apache.org/aggregator.html">Aggregator</a>
030 * pattern where a batch of messages are processed (up to a maximum amount or
031 * until some timeout is reached) and messages for the same correlation key are
032 * combined together using some kind of {@link AggregationStrategy}
033 * (by default the latest message is used) to compress many message exchanges
034 * into a smaller number of exchanges.
035 * <p/>
036 * A good example of this is stock market data; you may be receiving 30,000
037 * messages/second and you may want to throttle it right down so that multiple
038 * messages for the same stock are combined (or just the latest message is used
039 * and older prices are discarded). Another idea is to combine line item messages
040 * together into a single invoice message.
041 *
042 * @version $Revision: 788621 $
043 */
044 public class Aggregator extends BatchProcessor implements Traceable {
045
046 private Expression correlationExpression;
047
048 public Aggregator(Processor processor, Expression correlationExpression, AggregationStrategy aggregationStrategy) {
049 this(processor, new DefaultAggregationCollection(correlationExpression, aggregationStrategy));
050 this.correlationExpression = correlationExpression;
051 }
052
053 public Aggregator(Processor processor, Expression correlationExpression, AggregationStrategy aggregationStrategy,
054 Predicate aggregationCompletedPredicate) {
055 this(processor, new PredicateAggregationCollection(correlationExpression, aggregationStrategy, aggregationCompletedPredicate));
056 this.correlationExpression = correlationExpression;
057 }
058
059 public Aggregator(Processor processor, AggregationCollection collection) {
060 super(processor, collection);
061 }
062
063 @Override
064 public String toString() {
065 return "Aggregator[to: " + getProcessor() + "]";
066 }
067
068 public String getTraceLabel() {
069 return "Aggregate[" + correlationExpression + "]";
070 }
071 }