骆驼拆分/汇总并合并列表<a> that contains List<b> that contains List<c>

时间:2019-02-05 15:22:17

标签: spring-boot apache-camel dsl

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 个答案:

答案 0 :(得分:0)

我将在拆分之前将主体A捕获为交换属性,然后稍后再使用。

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