camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "kraythe ." <kray...@gmail.com>
Subject Re: Aggregator, beyond the simple string example
Date Mon, 23 Dec 2013 21:35:11 GMT
I think I can share this one. .. its a pretty mundane one I've used in more
than one company.

public class ListAggregationStrategy<T> implements AggregationStrategy {
  /** The comparator used to keep the list sorted if any. */
  final Comparator<T> comparator;

  /**
   * @param comparator The comparator used to keep the list sorted if any;
if this is null then the list will not be kept sorted.
   */

  public ListAggregationStrategy(final Comparator<T> comparator) {
    this.comparator = comparator;
  }

  @Override
  public Exchange aggregate(final Exchange aggregatedExchange,
finalExchange newExchange) {
    final T newBody = (T) newExchange.getIn().getBody();
    List<T> exchangeBodies = null;
    if (aggregatedExchange == null) {
      exchangeBodies = new LinkedList<>();
      exchangeBodies.add(newBody);
      newExchange.getIn().setBody(exchangeBodies);
      return newExchange;
    }
    exchangeBodies = aggregatedExchange.getIn().getBody(List.class);
    exchangeBodies.add(newBody);
    if (null != this.comparator) Collections.sort(exchangeBodies, this.
comparator);
    return aggregatedExchange;
  }
}

*Robert Simmons Jr. MSc. - Lead Java Architect @ EA*
*Author of: Hardcore Java (2003) and Maintainable Java (2012)*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39
<http://www.linkedin.com/pub/robert-simmons/40/852/a39>*


On Thu, Dec 19, 2013 at 7:39 AM, Cecilio Alvarez <
cecilio.alvarez@hotmail.com> wrote:

> Hi,
>
> I did long time ago a test to add a child node to an xml root, you could
> modify it.
>
> public class XmlAggregator implements AggregationStrategy {
>
>         public Exchange aggregate(Exchange oldExchange, Exchange
> newExchange) {
>
>             Document oldBody =
> oldExchange.getIn().getBody(org.w3c.dom.Document.class);
>             Document newBody =
> newExchange.getIn().getBody(org.w3c.dom.Document.class);
>
>             NodeList list = newBody.getElementsByTagName("tag");
>
>             Element element = (Element) list.item(0);
>
>             Node copiedNode = oldBody.importNode(element, true);
>
>             oldBody.getDocumentElement().appendChild(copiedNode);
>
>             oldExchange.getIn().setBody(oldBody);
>
>         return oldExchange;
>     }
> }
>
> Hope it help.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Aggregator-beyond-the-simple-string-example-tp5745012p5745055.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message