首页 文章

如何在camel routebuilder中拆分对象

提问于
浏览
0

大家好,我是Java,骆驼等新手 . 这是我的问题:

我的代码将包含订单商品的订单和xml格式的其他信息从一个camel处理器传递到另一个 . 然后,此特定处理器拆分订单并创建多个订单,然后将它们全部作为单独的消息传递到下一个 endpoints .

目前,此处理器使用ProducerTemplate显式实现此目的 . 我想将此行为移至RouteBuilder本身,而不是使用ProducerTemplate . 我看过Splitter和MessageTranslator,但我认为我没有所有的部分 .

所以基本上我想使用Splitter在RouteBuilder中拆分消息,但是我想提供将接收消息的自定义代码,然后将其反序列化为Order对象,然后创建多个Order对象,然后将它们作为单独的消息发送到下一个终点 . 我该如何做到这一点?

例如我希望能够做类似的事情

from("startPoint").split(MyCustomStrategy).to("endPoint")

//where MyCustomStrategy will take the message, 
//and split it up and pass all the newly created messages to endPoint.

1 回答

  • 2

    您可以在路径中使用bean或处理器来创建 Order 对象并将它们作为集合返回(例如, List<Order> 或类似) . 然后可以使用分离器EIP处理该集合中的每个 Order ,一次一个,例如,将每个订单传递给处理单个订单的另一个处理器/ bean,可能根据需要继续到另一个 endpoints ,等等 .

    // Pseudocode:
    from(input).
    to(bean-which-returns-a-collection-of-orders).
    split(on-the-body).
    to(bean-which-processes-a-single-order).
    to(anywhere-else-needed-for-your-purposes).
    // etc...
    

    或类似的规定;对不起,我使用Spring DSL而不是Java DSL,但是camel docs显示了两者 . 这是一些实际的Spring DSL代码,其中一个集合被拆分以处理集合中的每个项目:

    <split>
                  <simple>${body}</simple>
                  <doTry>
                     <log message="A.a1 -- splitting batches for transfer" loggingLevel="DEBUG" />
                     <setHeader headerName="currentBatchNumber">
                        <simple>${body.batchNumber}</simple>
                     </setHeader>
                     <setHeader headerName="CamelFileName">
                        <simple>${body.batchNumber}.xml</simple>
                     </setHeader>
                     <log message="A.a2 -- marshalling a single batch to XML" loggingLevel="DEBUG" />
                     <marshal>
                         <jaxb prettyPrint="true" contextPath="generated.gov.nmcourts.ecitation"
                           partClass="generated.gov.nmcourts.ecitation.NMCitationEFileBatch"
                           partNamespace="EFileBatch" />
                     </marshal>
    
                     <log message="A.a3 -- xslt transform to add schema location" loggingLevel="DEBUG" />
                     <to uri="{{addSchemaLocationXsltUri}}"/>
    
                     <log message="A.a4 -- ftp now initiating" loggingLevel="DEBUG" />
                     <log message="ftping $simple{in.header.CamelFileName}" loggingLevel="DEBUG"/>
    
                     <bean ref="markFtpStatusOnTickets"/>
                     <to uri="{{ftpOdysseyInputPath}}"/>
                     <log message="A.a5 -- ftp now complete" loggingLevel="DEBUG" />
                     <doCatch>
                        <exception>java.lang.Exception</exception>
                        <handled>
                           <constant>true</constant>
                        </handled>
                        <bean ref="ticketExceptionHandler" method="handleException"/>
                     </doCatch>
                  </doTry>
              </split>
    

相关问题