Class FlexibleAggregationStrategy<E>

  • All Implemented Interfaces:
    org.apache.camel.AggregationStrategy

    public class FlexibleAggregationStrategy<E>
    extends Object
    implements org.apache.camel.AggregationStrategy
    The Flexible Aggregation Strategy is a highly customizable, fluently configurable aggregation strategy. It 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:

    • Filtering results based on a defined Predicate written in any language, such as XPath, OGNL, Groovy, Simple, etc.
    • Picking specific data elements for aggregation.
    • Accumulating results in any designated Collection type, e.g. in a HashSet, LinkedList, ArrayList, etc.
    • Storing the output in a specific place in the Exchange: a property, a header or in the body.
    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.

    • 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​(org.apache.camel.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​(org.apache.camel.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.
      • aggregate

        public org.apache.camel.Exchange aggregate​(org.apache.camel.Exchange oldExchange,
                                                   org.apache.camel.Exchange newExchange)
        Specified by:
        aggregate in interface org.apache.camel.AggregationStrategy
      • timeout

        public void timeout​(org.apache.camel.Exchange oldExchange,
                            int index,
                            int total,
                            long timeout)
        Specified by:
        timeout in interface org.apache.camel.AggregationStrategy
      • onCompletion

        public void onCompletion​(org.apache.camel.Exchange exchange)
        Specified by:
        onCompletion in interface org.apache.camel.AggregationStrategy