问题
I'm trying to conditionally split an exchange into its contents if it is a List, otherwise leave it as a single item and have both go to the same processor
I ideally do not want to set up lots of inbetween direct:endpoints to achieve this
from( X )
.when( body().isInstanceOf( List.class )
.split( body() )
.setHeader( "x", constant( "I don't care " ) // this needs to be set as split must have at least one child node
.process( ? ) // here the exchange.in.body is now a single item from the List // this is what I want to continue outside of this when block
.end() // also tried .end().end() and .endChoice()
.process( ... ) // here the exchange in is a List again, I want it to be the single items, split
I do not understand why the exchange is not left as single units, that is how it becomes a List again when it leaves the when 'block'. I get the feeling Camel 'expects' me to use direct:endpoints to achieve this, but I find readability decreases quickly the more direct points are used and want to avoid them if possible.
回答1:
Currently the way camel's split component is setup only the code underneath a list will process as individual records. Once you close the split block you are back to a single exchange. The only way to break that up would be to leverage a mid point like a direct endpoint. If you want to break up the logic you can always create separate route builder classes. The other option is to request a feature on the forums.
回答2:
Use the aggregation strategy on the splitter to build the output you want from the splitter. There you can store the splitted sub message you want, instead of the original message that the splitter uses today.
来源:https://stackoverflow.com/questions/37663358/i-want-to-conditionally-split-a-list-if-exxchange-is-a-list-and-continue-process