以下示例代码适用于所有版本的Camel,不包括2.18.x
from("direct:process")
.process(new Processor() {
public void process(Exchange exchange) {
List<String> alist = new ArrayList<String>();
alist.add("1");
alist.add("99");
exchange.getIn().setHeader("ITEMS", alist);
exchange.getIn().setHeader("TOTAL_LOOPS", alist.size());
}
})
.loop(simple("${header.TOTAL_LOOPS}", Integer.class))
.setHeader("item", simple("${header.ITEMS[${property.CamelLoopIndex}]}", String.class))
.log(LoggingLevel.INFO, LOG_CLASS_NAME, simple("item = ${header.item} and TOTAL_MAPS = ${header.TOTAL_LOOPS}").getText())
.end()
.end();
但是对于2.18.x,会抛出以下异常:
2017-02-03 21:13:31错误DefaultErrorHandler:204 - 交付失败 for(MessageId:ID-CATL0W10D4DG4R1-55822-1486174410756-0-1 on ExchangeId:ID-CATL0W10D4DG4R1-55822-1486174410756-0-2)。累 交货后尝试:1抓住: org.apache.camel.language.bean.RuntimeBeanExpressionException:失败 在java.util.ArrayList上调用方法:[$ {property.CamelLoopIndex}] 由于:java.lang.IndexOutOfBoundsException:Key: 在bean中找不到$ {property.CamelLoopIndex}:[1,99]类型: java.util.ArrayList使用OGNL路径[[$ {property.CamelLoopIndex}]]