camel split/aggregate and merge List<A> that contains List<B> that contains List<C>

余生颓废 提交于 2019-12-13 03:47:38

问题


Given a structure similar to this one:

@Data
public class A {
    //..other fields..

    private List<B> bs;
}

@Data
public class B {
    //..other fields..

    private List<C> cs;
}

I have to process a list of type A in multiple steps/routes. Some operation are in the A level, others in other levels like C.

The problem I'm trying to solve is to process every single C given an A and then some logic afterwards have to work with the updated A model.

I can successfully split and aggregate back the list<C> but now i'm stuck trying to rebuild A given the output for B and C.

This what I have at the moment:

from("direct:my-A-Item")
    .id("direct-a")
    .autoStartup(true)
    .split(ExpressionBuilder.beanExpression(new CSplitter(), "getBs"), new MyAggregationStrategy())
        .streaming()
        .split(ExpressionBuilder.beanExpression(new CSplitter(), "getCs"), new MyAggregationStrategy())
            .streaming()
            .bean(processor, "doStuff")//Working on a since C instance
        .end()
        .bean(processor, "test") //Here I get the worked List<C>
    .end()
    //.bean(processor, "thisProcessorNeedsA").end(); //TODO get the original A and the output List<C> so i can make further work on them

How can I update the B instances with the new list of C and then do the same to update A?

public class CSplitter {

    public List<B> getBs(A a) {
        return a.getBs();
    }

    public List<C> getCs(B b) {
        return b.getCs();
    }
}

public class MyAggregationStrategy implements AggregationStrategy {

    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        Object newBody = newExchange.getIn().getBody();
        ArrayList<Object> list = null;
        if (oldExchange == null) {
            list = new ArrayList<Object>();
            list.add(newBody);
            newExchange.getIn().setBody(list);
            return newExchange;
        } else {
            list = oldExchange.getIn().getBody(ArrayList.class);
            list.add(newBody);
            return oldExchange;
        }
    }
}

Checking the documentations and online resources i could not find any example aggregating having also the body from a previous step... any tips is welcome :)


回答1:


I would capture the body A as an exchange property before splitting, then it will be available later.

from("direct:my-A-Item")
    .id("direct-a")
    .autoStartup(true)
    .setProperty("originalA", body())
    .split
      // etc.
    .end()
    .bean(processor, "myMethod(${property.originalA}, ${body})").end(); 


来源:https://stackoverflow.com/questions/54537685/camel-split-aggregate-and-merge-lista-that-contains-listb-that-contains-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!