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.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: 36635 $ 043 */ 044 public class Aggregator extends BatchProcessor { 045 private Predicate aggregationCompletedPredicate; 046 047 public Aggregator(Endpoint endpoint, Processor processor, Expression correlationExpression, 048 AggregationStrategy aggregationStrategy) { 049 this(endpoint, processor, new AggregationCollection(correlationExpression, aggregationStrategy)); 050 } 051 052 public Aggregator(Endpoint endpoint, Processor processor, Expression correlationExpression, 053 AggregationStrategy aggregationStrategy, Predicate aggregationCompletedPredicate) { 054 this(endpoint, processor, new PredicateAggregationCollection(correlationExpression, aggregationStrategy, aggregationCompletedPredicate)); 055 this.aggregationCompletedPredicate = aggregationCompletedPredicate; 056 } 057 058 public Aggregator(Endpoint endpoint, Processor processor, AggregationCollection collection) { 059 super(endpoint, processor, collection); 060 } 061 062 @Override 063 public String toString() { 064 return "Aggregator[to: " + getProcessor() + "]"; 065 } 066 067 @Override 068 protected boolean isBatchCompleted(int index) { 069 if (aggregationCompletedPredicate != null) { 070 if (getCollection().size() > 0) { 071 return true; 072 } 073 } 074 return super.isBatchCompleted(index); 075 } 076 }