Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

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

So, say we have the follwing (simplified) route setup:

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();

The thing is, I can see that this works with or without "addsAListToBody" Processor. It we pass a normal object of type Foo it passes on the exchange to "oneMessageAtATime" Processor. If the route contains "addsAListToBody" Processor that returns a List<Foo> then it splits the list into Foo elements that get processed one by one.

The thing is, this seems like a very simple thing that solves what I want to do but for some reason it feels like it's wrong.

Is this "conditional" useage of splitter correct? Or am I doing something wrong here?

question from:https://stackoverflow.com/questions/65626386/is-it-safe-to-use-split-body-when-the-exchange-may-or-may-not-contain-an-ite

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is totally OK, nothing wrong here (as your findings confirm).

  • if the body contains something "splittable" (iterable), it is splitted
  • if not, the whole body is passed on (it is treated like a single element list)

This is very convenient since you do not need to create pseudo lists from single objects before passing them to the splitter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...