我知道有很多问题已经提出,并且没有一个问题对我的情况有所帮助。
这里我试图在Split的第一次迭代中将一些值设置为Header,然后在拆分的第二次迭代中,我需要旧的交换值将其附加到当前值。
代码:SomeDummyClass2.java
List<String> valueHeader = (List<String>) exchange.getIn().getHeader(SOME_HEADER_KEY);
// first iteration it will be null
if (CollectionUtils.isEmpty(valueHeader)) {
exchange.getIn().setHeader(SOME_HEADER_KEY, "dummyValue");
} else {
// in the second iteration it should come here , but it is not comming as 'valueHeader' is null.
someValue.addAll(valueHeader);
exchange.getIn().setHeader(SOME_HEADER_KEY, "dummyValue");
}
骆驼路线:
<routes xmlns="http://camel.apache.org/schema/spring" id="route_id">
<route>
<from uri="somedummy_route" />
<to uri="SomeDummyClass1" />
<split>
<simple>${body}</simple>
<to uri="bean:SomeDummyClass2" />
</split>
</route>
</routes>
我已经解决了他们要求使用CamelSplitIndex或AggregationStrategy的其他一些问题,但我是新手,有人可以帮助我。
答案 0 :(得分:2)
是的,我们可以使用AggregationStrategy。我为您的案例创建了一个示例工作路线。它在java dsl中。但战略是一样的。
from("direct://somedummy_route").to("bean:SomeDummyClass1?method=SomeDummyMethod")
.split(simple("${body}"))
.aggregationStrategy(new MyStrategy())
.to("bean:SomeDummyClass2?method=SomeDummyMethod2")
;
在XML
中<routes xmlns="http://camel.apache.org/schema/spring" id="route_id">
<route>
<from uri="somedummy_route" />
<to uri="bean:SomeDummyClass1?method=SomeDummyMethod" />
<split>
<simple>${body}</simple>
<aggregate strategyRef="aggregatorStrategy">
<to uri="bean:SomeDummyClass2?method=SomeDummyMethod2" />
</aggregate>
</split>
</route>
</routes>
<bean id="aggregatorStrategy" class="org.own.package.MyStrategy"/>
策略类看起来像这样
public class MyStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
List<String> someValue = Lists.newArrayList();
if(oldExchange == null) {
List<String> valueHeader = (List<String>) newExchange.getIn().getHeader("SomeKey");
if (CollectionUtils.isEmpty(valueHeader)) {
newExchange.getIn().setHeader("SomeKey", Lists.newArrayList("dummyValue"));
}
return newExchange;
}
List<String> valueHeader = (List<String>) oldExchange.getIn().getHeader("SomeKey");
someValue.addAll(valueHeader);
return oldExchange;
}
}