Apache camel 当交换的正文中可能包含或可能不包含iterable时,使用split().body()安全吗?

Apache camel 当交换的正文中可能包含或可能不包含iterable时,使用split().body()安全吗?,apache-camel,Apache Camel,因此,假设我们有以下(简化)路线设置: RouteDefination routeDef = from("seda:somewhereIrrelevant") .process(someProcessor1); // normal message. if ( typeOfRoute.equals("requiresList"){ routeDef.process(addsAListTo

因此,假设我们有以下(简化)路线设置:

RouteDefination routeDef = from("seda:somewhereIrrelevant")
                               .process(someProcessor1); // normal message.

if ( typeOfRoute.equals("requiresList"){
    routeDef.process(addsAListToBody); // creates list out of normal message.
}

routeDef.split().body()
        .process(oneMessageAtATime) // this is regardless of whether it was originally a part of a list.
        .end();
问题是,我可以看出,无论是否使用
“addsAListToBody”
处理器,这都是可行的。如果我们传递一个类型为
Foo
的普通对象,它将在交换中传递给“OneMessageAtTime”处理器。如果路由包含返回
列表的
“addsAListToBody”
处理器,则它会将列表拆分为
Foo
元素,并逐个处理

问题是,这似乎是一件非常简单的事情,解决了我想做的事情,但出于某种原因,它感觉好像是错的


拆分器的这种“有条件”使用是否正确?还是我在这里做错了什么?

这完全没问题,这里没有错(正如你的发现所证实的)

  • 如果主体包含“可拆分”(iterable)的内容,则它将被拆分
  • 如果不是,则传递整个主体(将其视为单个元素列表)
这非常方便,因为在将单个对象传递给对象之前,不需要从单个对象创建伪列表