org.apache.camel.util.toolbox
Class FlexibleAggregationStrategy<E>

java.lang.Object
  extended by org.apache.camel.util.toolbox.FlexibleAggregationStrategy<E>
All Implemented Interfaces:
AggregationStrategy, CompletionAwareAggregationStrategy, TimeoutAwareAggregationStrategy

public class FlexibleAggregationStrategy<E>
extends Object
implements AggregationStrategy, CompletionAwareAggregationStrategy, TimeoutAwareAggregationStrategy

The Flexible Aggregation Strategy is a highly customizable, fluently configurable aggregation strategy. It allows you to quickly allows you to quickly whip up an AggregationStrategy that is capable of performing the most typical aggregation duties, with zero Java code.

It can perform the following logic:

It also includes the ability to specify both aggregation batch completion actions and timeout actions, in an abbreviated manner.

This Aggregation Strategy is suitable for usage in aggregate, split, multicast, enrich and recipient list EIPs.


Nested Class Summary
static interface FlexibleAggregationStrategy.CompletionAwareMixin
           
static interface FlexibleAggregationStrategy.TimeoutAwareMixin
           
 
Constructor Summary
FlexibleAggregationStrategy()
          Initializes a new instance with Object as the castAs type.
FlexibleAggregationStrategy(Class<E> type)
          Initializes a new instance with the specified type as the castAs type.
 
Method Summary
 FlexibleAggregationStrategy<E> accumulateInCollection(Class<? extends Collection> collectionType)
          Accumulate the result of the pick expression in a collection of the designated type.
 Exchange aggregate(Exchange oldExchange, Exchange newExchange)
          Aggregates an old and new exchange together to create a single combined exchange
 FlexibleAggregationStrategy<E> castAs(Class<E> castAs)
          Cast the result of the pick expression to this type.
 FlexibleAggregationStrategy<E> completionAware(FlexibleAggregationStrategy.CompletionAwareMixin completionMixin)
          Plugs in logic to execute when an aggregation batch completes.
 FlexibleAggregationStrategy<E> condition(Predicate predicate)
          Set a filter condition such as only results satisfying it will be aggregated.
 FlexibleAggregationStrategy<E> ignoreInvalidCasts()
          Ignores invalid casts instead of throwing an exception if the pick expression result cannot be casted to the specified type.
 void onCompletion(Exchange exchange)
          The aggregated Exchange has completed Important: This method must not throw any exceptions.
 FlexibleAggregationStrategy<E> pick(Expression expression)
          Set an expression to extract the element to be aggregated from the incoming Exchange.
 FlexibleAggregationStrategy<E> storeInBody()
          Store the result of this Aggregation Strategy (whether an atomic element or a Collection) in the body of the IN message.
 FlexibleAggregationStrategy<E> storeInHeader(String headerName)
          Store the result of this Aggregation Strategy (whether an atomic element or a Collection) in an IN message header with the designated name.
 FlexibleAggregationStrategy<E> storeInProperty(String propertyName)
          Store the result of this Aggregation Strategy (whether an atomic element or a Collection) in a property with the designated name.
 FlexibleAggregationStrategy<E> storeNulls()
          Enables storing null values in the resulting collection.
 void timeout(Exchange oldExchange, int index, int total, long timeout)
          A timeout occurred.
 FlexibleAggregationStrategy<E> timeoutAware(FlexibleAggregationStrategy.TimeoutAwareMixin timeoutMixin)
          Plugs in logic to execute when a timeout occurs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FlexibleAggregationStrategy

public FlexibleAggregationStrategy()
Initializes a new instance with Object as the castAs type.


FlexibleAggregationStrategy

public FlexibleAggregationStrategy(Class<E> type)
Initializes a new instance with the specified type as the castAs type.

Parameters:
type - The castAs type.
Method Detail

pick

public FlexibleAggregationStrategy<E> pick(Expression expression)
Set an expression to extract the element to be aggregated from the incoming Exchange. All results are cast to the castAs type (or the type specified in the constructor).

By default, it picks the full IN message body of the incoming exchange.

Parameters:
expression - The picking expression.
Returns:
This instance.

condition

public FlexibleAggregationStrategy<E> condition(Predicate predicate)
Set a filter condition such as only results satisfying it will be aggregated. By default, all picked values will be processed.

Parameters:
predicate - The condition.
Returns:
This instance.

accumulateInCollection

public FlexibleAggregationStrategy<E> accumulateInCollection(Class<? extends Collection> collectionType)
Accumulate the result of the pick expression in a collection of the designated type. No nulls will stored unless the storeNulls() option is enabled.

Parameters:
collectionType - The type of the Collection to aggregate into.
Returns:
This instance.

storeInProperty

public FlexibleAggregationStrategy<E> storeInProperty(String propertyName)
Store the result of this Aggregation Strategy (whether an atomic element or a Collection) in a property with the designated name.

Parameters:
propertyName - The property name.
Returns:
This instance.

storeInHeader

public FlexibleAggregationStrategy<E> storeInHeader(String headerName)
Store the result of this Aggregation Strategy (whether an atomic element or a Collection) in an IN message header with the designated name.

Parameters:
headerName - The header name.
Returns:
This instance.

storeInBody

public FlexibleAggregationStrategy<E> storeInBody()
Store the result of this Aggregation Strategy (whether an atomic element or a Collection) in the body of the IN message.

Returns:
This instance.

castAs

public FlexibleAggregationStrategy<E> castAs(Class<E> castAs)
Cast the result of the pick expression to this type.

Parameters:
castAs - Type for the cast.
Returns:
This instance.

storeNulls

public FlexibleAggregationStrategy<E> storeNulls()
Enables storing null values in the resulting collection. By default, this aggregation strategy will drop null values.

Returns:
This instance.

ignoreInvalidCasts

public FlexibleAggregationStrategy<E> ignoreInvalidCasts()
Ignores invalid casts instead of throwing an exception if the pick expression result cannot be casted to the specified type. By default, this aggregation strategy will throw an exception if an invalid cast occurs.

Returns:
This instance.

timeoutAware

public FlexibleAggregationStrategy<E> timeoutAware(FlexibleAggregationStrategy.TimeoutAwareMixin timeoutMixin)
Plugs in logic to execute when a timeout occurs.

Parameters:
timeoutMixin -
Returns:
This instance.

completionAware

public FlexibleAggregationStrategy<E> completionAware(FlexibleAggregationStrategy.CompletionAwareMixin completionMixin)
Plugs in logic to execute when an aggregation batch completes.

Parameters:
completionMixin -
Returns:
This instance.

aggregate

public Exchange aggregate(Exchange oldExchange,
                          Exchange newExchange)
Description copied from interface: AggregationStrategy
Aggregates an old and new exchange together to create a single combined exchange

Specified by:
aggregate in interface AggregationStrategy
Parameters:
oldExchange - the oldest exchange (is null on first aggregation as we only have the new exchange)
newExchange - the newest exchange (can be null if there was no data possible to acquire)
Returns:
a combined composite of the two exchanges

timeout

public void timeout(Exchange oldExchange,
                    int index,
                    int total,
                    long timeout)
Description copied from interface: TimeoutAwareAggregationStrategy
A timeout occurred.

Important: This method must not throw any exceptions.

Specified by:
timeout in interface TimeoutAwareAggregationStrategy
Parameters:
oldExchange - the current aggregated exchange, or the original Exchange if no aggregation has been done before the timeout occurred
index - the index, may be -1 if not possible to determine the index
total - the total, may be -1 if not possible to determine the total
timeout - the timeout value in millis, may be -1 if not possible to determine the timeout

onCompletion

public void onCompletion(Exchange exchange)
Description copied from interface: CompletionAwareAggregationStrategy
The aggregated Exchange has completed Important: This method must not throw any exceptions.

Specified by:
onCompletion in interface CompletionAwareAggregationStrategy
Parameters:
exchange - the current aggregated exchange, or the original Exchange if no aggregation has been done before the completion occurred


Apache Camel