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.Endpoint;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Predicate;
022    import org.apache.camel.Processor;
023    import org.apache.camel.processor.aggregate.AggregationCollection;
024    import org.apache.camel.processor.aggregate.AggregationStrategy;
025    import org.apache.camel.processor.aggregate.DefaultAggregationCollection;
026    import org.apache.camel.processor.aggregate.PredicateAggregationCollection;
027    
028    /**
029     * An implementation of the <a
030     * href="http://activemq.apache.org/camel/aggregator.html">Aggregator</a>
031     * pattern where a batch of messages are processed (up to a maximum amount or
032     * until some timeout is reached) and messages for the same correlation key are
033     * combined together using some kind of {@link AggregationStrategy}
034     * (by default the latest message is used) to compress many message exchanges
035     * into a smaller number of exchanges.
036     * <p/>
037     * A good example of this is stock market data; you may be receiving 30,000
038     * messages/second and you may want to throttle it right down so that multiple
039     * messages for the same stock are combined (or just the latest message is used
040     * and older prices are discarded). Another idea is to combine line item messages
041     * together into a single invoice message.
042     *
043     * @version $Revision: 702683 $
044     */
045    public class Aggregator extends BatchProcessor {
046        private Predicate aggregationCompletedPredicate;
047    
048        public Aggregator(Endpoint endpoint, Processor processor, Expression correlationExpression,
049                          AggregationStrategy aggregationStrategy) {
050            this(endpoint, processor, new DefaultAggregationCollection(correlationExpression, aggregationStrategy));
051        }
052    
053        public Aggregator(Endpoint endpoint, Processor processor, Expression correlationExpression,
054                          AggregationStrategy aggregationStrategy, Predicate aggregationCompletedPredicate) {
055            this(endpoint, processor, new PredicateAggregationCollection(correlationExpression, aggregationStrategy, aggregationCompletedPredicate));
056            this.aggregationCompletedPredicate = aggregationCompletedPredicate;
057        }
058    
059        public Aggregator(Endpoint endpoint, Processor processor, AggregationCollection collection) {
060            super(endpoint, processor, collection);
061        }
062    
063        @Override
064        public String toString() {
065            return "Aggregator[to: " + getProcessor() + "]";
066        }
067    
068        @Override
069        protected boolean isBatchCompleted(int index) {
070            if (aggregationCompletedPredicate != null) {
071                // TODO: (davsclaus) What is the point with this code? I think its wrong
072                if (getCollection().size() > 0) {
073                    return true;
074                }
075            }
076    
077            return super.isBatchCompleted(index);
078        }
079    }