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://activemq.apache.org/camel/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: 64696 $
043     */
044    public class Aggregator extends BatchProcessor {
045        private Predicate aggregationCompletedPredicate;
046    
047        public Aggregator(Processor processor, Expression correlationExpression,
048                          AggregationStrategy aggregationStrategy) {
049            this(processor, new DefaultAggregationCollection(correlationExpression, aggregationStrategy));
050        }
051    
052        public Aggregator(Processor processor, Expression correlationExpression,
053                          AggregationStrategy aggregationStrategy, Predicate aggregationCompletedPredicate) {
054            this(processor, new PredicateAggregationCollection(correlationExpression, aggregationStrategy, aggregationCompletedPredicate));
055            this.aggregationCompletedPredicate = aggregationCompletedPredicate;
056        }
057    
058        public Aggregator(Processor processor, AggregationCollection collection) {
059            super(processor, collection);
060        }
061    
062        @Override
063        public String toString() {
064            return "Aggregator[to: " + getProcessor() + "]";
065        }
066    
067    }