当交换可能包含或不包含主体中的可迭代对象时,使用 split().body() 是否安全?

Is it safe to use split().body() when the exchange may or may not contain an iterable in the body?

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

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 类型的普通对象,它会将交换传递给“oneMessageAtATime”处理器。如果路由包含 "addsAListToBody" 个 returns 一个 List<Foo> 的处理器,那么它会将列表拆分为 Foo 个元素,并逐一处理。

问题是,这似乎是一件非常简单的事情,可以解决我想做的事情,但出于某种原因,我觉得它是错误的。

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

这完全没问题,这里没有任何问题(如您的调查结果所证实)。

  • 如果正文包含“可拆分”(可迭代)的内容,它将被拆分
  • 如果不是,则整体传递(作为单元素列表处理)

这非常方便,因为您不需要在将单个对象传递给 splitter 之前从单个对象创建伪列表。